Using EXCEL VBA, I want to determine the number of non-blank rows in a column and put that value into a variable.

I have a list of parts in a single column in a spreadsheet. I would like to count the number of parts in the column and put that value into a variable.
EdLBAsked:
Who is Participating?
 
Martin LissOlder than dirtCommented:
Or a touch more efficiently

With Worksheets("Sheet1")
    MyVariable = Application.CountA(.Range("A2:A1" & .UsedRange.Rows.Count))
End With

Open in new window

0
 
byundtCommented:
MyVariable = Application.CountA(Worksheets("Sheet1").Range("A2:A10000"))
0
 
Martin LissOlder than dirtCommented:
MyVariable = Application.CountA(Worksheets("Sheet1").Range("A2:A10000")) would ignore data if there are more than 10000 rows. Do this if that's a possibility.

MyVariable = Application.CountA(Worksheets("Sheet1").Range("A2:A1" & Worksheets("Sheet1").UsedRange.Rows.Count))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
EdLBAuthor Commented:
I used MyVariable = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row
0
 
EdLBAuthor Commented:
I realize that's not the same thing exactly that I asked for but it suffices for what I am trying to do. If there are non-blank rows in the list, the row number provides the count of items in the list. That is a precondition for the list, which I failed to mention in my question.

Thanks for the help.
0
 
Martin LissOlder than dirtCommented:
I'm Requesting Attention for this thread because while I could have come up with byundt's code on my own, I did use it in my solution so I propose that the points be split between us.

@EdLB: If you can explain what you mean when you say "the row number provides the count of items in the list", perhaps I or byundt can give you what you really want.
0
 
EdLBAuthor Commented:
Since there are no blanks rows between the first row in the column and the last row of the column, the count of the rows equals the count of items (minus 1 header row) Splitting points works for me. Not sure how to split the points since they've been designated already.

However Martin, after reviewing your and code, I went with it because it provides the number of non-blank entries in the column even if there are blank rows.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.