Solved

Record count in Excel MARCO VBA

Posted on 2001-06-20
14
551 Views
Last Modified: 2008-02-01
How do I get a record count in Excel???  I just want to know how many records are in a sheet??
Thanks
0
Comment
Question by:strongd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +3
14 Comments
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6211550
Hi,
Some people use UsedRange but i simply add a function:

Function LastRow()
  For i=1 to 32000
   Cells(i,1).Select
   a = ActiveCell.Value
   if a <> "" then
   LastRow=a-1
   Exit Sub
  Next
End Function

Enjoy
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6211581
Hi,
Sorry:
Some people use UsedRange but i simply add a function:

Function LastRow()
 For i=1 to 32000
  Cells(i,1).Select
  a = ActiveCell.Value
  if a <> "" then
     LastRow=a-1
     Exit Function
  End If
 Next
End Function

Enjoy
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6211591
Hi,
Bad day:
Some people use UsedRange but I simply add a function:

Function LastRow()
  For i=1 to 32000
   Cells(i,1).Select
   a = ActiveCell.Value
   If a = "" then
    LastRow=a-1
    Exit Function
   End If
  Next
End Function

Enjoy
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 3

Expert Comment

by:jrspano
ID: 6211676
what happens if some one happens to skip a line?
0
 
LVL 2

Expert Comment

by:nfernand
ID: 6211682
Hi guys... anothe Idea.

1) Use the CountA function to know how many cells in a range are not empty: =counta(A1:A2000) returns de number of non-empty cells between A1 and A2000.

2) In code... the same thing:

public function RecNumber() as double
RecNumber= Application.WorkSheetFunction.CountA(Range("A1:A2000")
end Function

Bye
0
 
LVL 2

Expert Comment

by:nfernand
ID: 6211685
Hi guys... anothe Idea.

1) Use the CountA function to know how many cells in a range are not empty: =counta(A1:A2000) returns de number of non-empty cells between A1 and A2000.

2) In code... the same thing:

public function RecNumber() as double
RecNumber= Application.WorkSheetFunction.CountA(Range("A1:A2000")
end Function

Bye
0
 
LVL 2

Accepted Solution

by:
nfernand earned 50 total points
ID: 6211688
Hi guys... anothe Idea.

1) Use the CountA function to know how many cells in a range are not empty: =counta(A1:A2000) returns de number of non-empty cells between A1 and A2000.

2) In code... the same thing:

public function RecNumber() as double
RecNumber= Application.WorkSheetFunction.CountA(Range("A1:A2000")
end Function

Bye
0
 
LVL 2

Expert Comment

by:nfernand
ID: 6211691
Opps... sorry.
0
 
LVL 2

Expert Comment

by:chandukb
ID: 6211722
ping:
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6211776
Try this

Worksheet.Rows.Count
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6211840
Hi,


Worksheet.Rows.Count gives 65,536 and is incorrect

public function RecNumber() as double
RecNumber= Application.WorkSheetFunction.CountA(Range("A1:A2000")
end Function

is also OK

Enjoy
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6211959
Sorry

Worksheet.Rows.Count

doesn't really work
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6212365
If you have for sure that blanks rows are not allowed, try this:

Function CountRecs() As Long

Range("A1").Select
Range("A1", Range("A1").End(xlDown)).Select
'MsgBox Selection.Cells.Count
CountRecs = Selection.Cells.Count

End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6266943
With this solution, you need to hard-code the range to count.
Cheers
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question