5

I am developing a dashboard in excel. And I am looking for calculating row count. (How many records are present) ..

Since there are some blank cells I thought to go from bottom to up. I use the following

   Range("A1048576").Select
Selection.End(xlUp).Select

After this execution the active cell is at A113 which means the row count is 113.

My question is how to get this number 113 from the active cell?

4 Answers 4

6

You can use this:

Dim lastrow as Long
lastrow = Cells(Rows.Count,"A").End(xlUp).Row

lastrow will contain number of last empty row in column A, in your case 113

Sign up to request clarification or add additional context in comments.

1 Comment

@Dmitry Pavliv How can this be adapted to perform the same function for counting the number of columns instead of rows?
4

The best way to get the count of rows/records (in most cases) is to use .UsedRange.Rows.Count. You can assign the return value to a variable like this:

lastRow = Sheets(1).UsedRange.Rows.Count

If you use a function that includes a column (such as column A) as shown in other examples, that will only get you the count of rows in that column, which may or may not be what you're going for. One caveat: if you have formatted rows below your last row with a value then it will return that row number.

Comments

2

Here is what I usually use for that:

lastrow = WorksheetFunction.CountA(Columns("A:A"))

This will return the number of non-empty cells in Column "A" which is what I think you're after. Hope this helps.

2 Comments

Hi Jim, Actually This wont work for me.. For ex if I remove cellA12 content... it will return me 112 .. But I wanted last row... But really thanks for your response :)
Opps, sorry about that. I must have misunderstood. Well, in any event, should you ever need a function like that, there it is. Take care. And good luck with your project!
0

If there is a slight chance that the last row of the worksheet is not empty, you should add an IsEmpty() check to @simoco 's solution. Therefore; following is a function that returns the last used row and check if the last row of the worksheet is empty:

Function lastRow(WS As Worksheet, iColumn As String) As Long

    If Not IsEmpty(WS.Range(iColumn & WS.Rows.Count)) Then
        lastRow = WS.Rows.Count
    Else
        lastRow = WS.Range(iColumn & WS.Rows.Count).End(xlUp).Row
    End If

End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.