Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with Excel macro counter 1

Status
Not open for further replies.

PJFry

Technical User
Feb 6, 2005
93
US
In a training worksheet, data is on lines 14-24
Below macro returns a count of 4 lines (but there are actually 11 lines of data.



Code:
Sub LineCnt()

    Cells(14, 1).Select
    
    Dim r As Integer
    r = 1
               
    Do While Cells(r, 1).Value <> ""
        r = r + 1
    Loop
      
    MsgBox ("Lines found: " & r)
    
End Sub

 



Hi,

First, [tt]Cells(14, 1).Select[/tt] does nothing for the count.

The cells that are counted are from A1 downward until an empty cell is encountered.

Skip,

[glasses] [red][/red]
[tongue]
 
Hi PJFry,

A couple of points:
. there is no need to select any cells; and
. your counter is initializing the count at row 1 (r = 1) and counting from there until the first empty cell is found.

Try something along the lines of:
Code:
Sub LineCnt()
    Dim StartRow As Integer
    Dim EndRow As Integer
    StartRow = 14
    EndRow = StartRow
    Do While Cells(EndRow, 1).Value <> ""
        EndRow = EndRow + 1
    Loop
    MsgBox ("Lines found: " & EndRow - StartRow)
End Sub
Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top