Count number of rows in multiple tabs to provide a total

philb99

Active Member
Joined
Feb 3, 2014
Messages
385
Office Version
  1. 2010
Platform
  1. Windows
Hi - I get sent multiple spreadsheets with multiple tabs with multiple rows of data in each - is there any way I can calculate how many rows of data are in each tab for each spreadsheet as it is received please to give a total for each spreadsheet

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If you create a FIRST sheet called Summary, this macro will count the rows in each sheet. You can then sum the B column in Summary for the total number of rows.

Code:
Sub CountRowsPerSheet()

Dim LR As Variant
Dim WS As Worksheet
Dim WS_Count As Integer
Dim i As Integer, j As Integer

WS_Count = ActiveWorkbook.Sheets.Count
For i = 2 To WS_Count
  LR = ActiveWorkbook.Sheets(i).UsedRange.Rows.Count
  With Sheets("Summary")
  .Cells(i, 2).Value = LR
  .Cells(i, 1).Value = Sheets(i).Name
  End With
Next i

End Sub

Note: The UsedRange could possibly include blank rows. Would that give you a mis-count or will your UsedRange(s) always be contiguous rows?

If the first column in all of your sheets will indicate the row count, you could change the line that creates LR (Last Row) to:

Code:
LR = ActiveWorkbook.Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row

And, you could put in the total automatically with:

Code:
Sub CountRowsPerSheet()

Dim LR As Variant
Dim WS As Worksheet
Dim WS_Count As Integer
Dim Total As Integer
Dim i As Integer, j As Integer
Total = 0
WS_Count = ActiveWorkbook.Sheets.Count
For i = 2 To WS_Count
  LR = ActiveWorkbook.Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row
' LR = ActiveWorkbook.Sheets(i).UsedRange.Rows.Count
  With Sheets("Summary")
  .Cells(i, 2).Value = LR
  .Cells(i, 1).Value = Sheets(i).Name
  Total = Total + LR
  End With
Next i
Sheets("Summary").Range("B1") = Total

End Sub
 
Last edited:
Upvote 0
This looks very comprehensive thank you

My tabs all have the same heading and there will be no blank rows - does this mean the best macro to be would be the first one with no changes?
 
Upvote 0
Hi - I have created a very simple spreadsheet with 3 rows of data - the first Macro only reported Summary = 1 and when I used the second macro Summary = either 1 or 2. Is there something else i need to be doing. Do all the tabs need to be called sheet(s)

Your help greatly appreciated
 
Upvote 0
That routine worked for me. Be sure that you have the Summary sheet as the FIRST sheet in your workbook.

The names of your sheet tabs doesn't matter.
 
Last edited:
Upvote 0
Ah I see now Summary has to be the first sheet - Perfect

I notice that it counts the Headers, is there anyway to remove please
 
Upvote 0
Change

Code:
LR = ActiveWorkbook.Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row

to

Code:
LR = ActiveWorkbook.Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row - 1
 
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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