VBA count rows

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
149
Office Version
  1. 365
Platform
  1. Windows
I need to count the number of rows in my pivot table. Normally, I would use: Count = Cells(Rows.Count, "A").End(xlUp).Row to get the number of rows with data in a column. I cannot figure out what to change to start counting at A4 and end at A24 in the example below. I can make the active cell A4 with Range("A4").Select, but don't know what to do from there. The number of rows will vary when I run the macro each time.

Any help or guidance is appreciated.

Screenshot 2024-08-28 at 6.38.55 PM.png
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
For your very specific situation

Excel Formula:
Count = Cells(Rows.Count, "A").End(xlUp).Row - 5

Your original expression gives the row number of the last nonempty cell. In this example it's row 25. One row is the Grand Total and four rows are headers, so subtract 5 to get the number of rows of data values, which in this example will give you 20.
 
Upvote 0
See if any of these are of interest. I am counting the grand total line at this stage.
Add "- 1" to the lastRow line if you don't want to include that line.

VBA Code:
Sub testRowCount()

    Dim ws As Worksheet
    Dim rng As Range
    Dim lastRow As Long
    Dim noOfRows As Long
  
    Set ws = ActiveSheet
  
    With ws
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row
      
        ' Option 1
        noOfRows = lastRow - 4
      
        ' Option 2
        Set rng = .Range("A5:A" & lastRow)
        noOfRows = rng.Rows.Count
      
        ' Option 3
        noOfRows = .Range("A5:A" & lastRow).Rows.Count
    End With

End Sub
 
Upvote 0
if the worksheet name is "Sheet1", the pivot table name is "PivotTable1",then here's the rows count:
VBA Code:
Sheet1.PivotTables("PivotTable1").DataFields(1).DataRange.Rows.Count
 
Upvote 1
Solution

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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