Count rows in VBA

Joy Liao

New Member
Joined
Jul 4, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hello guys!

I'd like to count how many rows with contents.
But I tried lots of methods, I got "the number of the bottom row".

Ex.
My data are like "A1,A3,A99", and I wanna get "3 rows", but turned into "99 rows" always.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
There are many ways to do that. Here are two:
VBA Code:
Sub CountRowsWithContents()

    Dim CellRange As Range
    Dim Method1 As Long, Method2 As Long

    With ActiveSheet
        Set CellRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With

    Method1 = Application.WorksheetFunction.CountA(CellRange)

    Method2 = CellRange.SpecialCells(xlCellTypeConstants).Count

    MsgBox "Method 1 = " & Method1 & vbCr & "Method 2 = " & Method2
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,299
Messages
6,177,754
Members
452,798
Latest member
mlance617

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top