Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > VB to count rows of an excel sheet


Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2008, 02:40 PM
delynchfam delynchfam is offline
Newcomer
 
Join Date: Jan 2008
Posts: 5
Question VB to count rows of an excel sheet


I am trying to get the last row of data number from an excel sheet. but it always returns row number of the last row 10200. the sheet I am using varies row numbers from 20 to some times as much as 19000.

Set xlApp = CreateObject("Excel.Application")
set xlBook = xlApp.WorkBooks.Open(objDialog.FileName)
set xlSht = xlApp.activesheet
'xlApp.DisplayAlerts = True
xlApp.Visible = True

lastrow = xlSht.UsedRange.Rows.Count

It doesn't seem to give me the used range lust the total
TIA
Reply With Quote
  #2  
Old 01-05-2008, 04:01 PM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

Do you have any formatting? Simply putting a border on a cell will count it in the used range
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies
Reply With Quote
  #3  
Old 01-05-2008, 04:13 PM
delynchfam delynchfam is offline
Newcomer
 
Join Date: Jan 2008
Posts: 5
Default formatting

no formatting on the file i recieve but if just putting a border around the rows and column (BTW about 8 column and ? rows , it varies) will correct it great
Please advise
TIA
Reply With Quote
  #4  
Old 01-05-2008, 04:16 PM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

No that wont solve it. I was just saying that formatting will count as the used range. Is there anything else in the file? Maybe a bit of data you forgot about at the bottom? Any colors? A white background counts as a color. The default is "no fill".
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies
Reply With Quote
  #5  
Old 01-05-2008, 04:29 PM
delynchfam delynchfam is offline
Newcomer
 
Join Date: Jan 2008
Posts: 5
Default

Hmm I dont know whats there. But I copied all the cells and pasted to a new workbook and now it shows 19 like its suppose to. Thanks
I dont know what I fixed but all is well. I thought that, code should have worked properly.
Thanks for the brain duster
Reply With Quote
  #6  
Old 01-05-2008, 04:42 PM
delynchfam delynchfam is offline
Newcomer
 
Join Date: Jan 2008
Posts: 5
Default Found it

Like your quote says about the obvious beign overlooked
1 column was centered text
doooh!
Reply With Quote
  #7  
Old 01-05-2008, 04:48 PM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

Good. Theres nothing worse than a problem that solves itself. If you find out what went wrong then you know for next time
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies
Reply With Quote
  #8  
Old 01-05-2008, 05:32 PM
cwcookman cwcookman is offline
Junior Contributor
 
Join Date: Dec 2004
Posts: 207
Default

I have seen the same thing when some cells have formatting outside of the data area. I have been able to clear the problem by

1)Deleting all rows and columns outside of the data area

or

2)Copying all the data to a new sheet.
Reply With Quote
  #9  
Old 01-07-2008, 10:41 AM
kdsdata kdsdata is offline
Newcomer
 
Join Date: Nov 2007
Posts: 15
Thumbs up alternative that works

I found this method (can't find the origin now, so I can't give proper credit, but it's not mine). Sorry I can't give credit to the code originator, but thanks anyway, the code works well.

It works very well, because it always looks for some value in the last cell.

copy (between dashes) to the declaration
===================
' create the row and col indicators outside.
' this makes them available anywhere
Public intFirstRow As Integer, intFirstCol As Integer, _
intLastRow As Integer, intLastCol As Integer
'
Public usedRng As Range
===================

copy (between dashes) as a sub
===================
' This sub receives the empty argument "usedRng" and determines
' the populated cells of the active worksheet, which is stored
' in the variable "theRng", and passed back
' to the calling sub (i.e usedRng, see below)
Sub DetermineUsedRange(ByRef theRng As Range)
On Error GoTo handleError
intFirstRow = Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByRows).Row
intFirstCol = Cells.Find(What:="*", _
SearchDirection:=xlNext, _
SearchOrder:=xlByColumns).Column
intLastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
intLastCol = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
Set theRng = Range(Cells(intFirstRow, intFirstCol), _
Cells(intLastRow, intLastCol))
handleError:
End Sub
===================

The call to the sub is simple
===================
' alway set usedRng to nothing prior to call
Set usedRng = Nothing
Call DetermineUsedRange(usedRng)
===================

Then intFirstRow, intFirstCol, intLastRow, intLastCol are available as true first and last pointer to "visible data".

I have never used usedRng, so I can't say how it works.

Be careful with active autofilters, they can confuse things.

Good Luck
kdsdata
Reply With Quote
  #10  
Old 01-08-2008, 03:43 PM
MPi MPi is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Dec 2001
Location: Quebec
Posts: 850
Default

To find the last row, you should use something like that
Code:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
This will give you the last row of column "A"


Another way is like this
Code:
Dim LastRow As Long, LastCell As String LastRow = Cells.Find("*", Range("A1"), , , xlByRows, xlPrevious).Row LastCell = Cells.Find("*", Range("A1"), , , xlByRows, xlPrevious).Address
This method has a little problem though... If the sheet is empty, it will return an error. But you can use it to find the last row, column or address of the whole Range. Just change the last word...

"UsedRange" is like other methods that I forgot and don't want to remember...
It remembers all the changes made in the sheet.
Let's say you have a Range of 100 lines. You go to the line 1000, write something and erase it. UsedRange will return 1000.
The methods I mentioned don't have that problem.

Hope it helps a bit...
__________________
MPi²
Reply With Quote
  #11  
Old 01-09-2008, 01:17 AM
kdsdata kdsdata is offline
Newcomer
 
Join Date: Nov 2007
Posts: 15
Talking an even better way to count rows

Code:
'__ To demonstrate how to count only visible rows
'__ in autofiltered data

'set up an Autofilter
With ActiveSheet

   'cancel possibly existing autofilter
   .AutoFilterMode = False

   'for this examle the data goes from A1 to IV1
   'and it's column 5 we want to autofilter
   'and it's only the "toaster"s we want in the list
   .Range("A1:IV1").AutoFilter
   .Range("A1:IV1").AutoFilter field:=5, Criteria1:="toaster"

End With
    
'initialize a range
Set rng = Range(Range("A1"), Range("A1").End(xlDown))

'extend the range to include the autofilter column
With rng.Resize(, 5)
   '-1 is needed on the next line to exclude the top (column name) row
   intNumberRows = rng.SpecialCells(xlCellTypeVisible).count - 1
End With
There is a problem with the (my) previous examples "IF" you want to use it on autofilter data.

In the code shown here, intNumberRows should give you the row count, excluding the invisble rows, of a range of filtered data.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->