Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A macro to Count the number of rows across all worksheets

Posted on 2016-09-23
3
Medium Priority
?
505 Views
Last Modified: 2016-09-23
Hi All,
Need some help in coming up with a VBA. A summary worksheet which would print all the worksheet names and counts of all the used rows(After row 7) of the worksheets. I also want to exclude the worksheets like table of contents.

example:
WorkBook  with worksheets: Instructions, Table of contents, Abc1, Abc2, Abc3, Abc4, ............

Summary Worksheet would display:
Worksheet     Counts
Abc1                     20
Abc2                   100    
Abc3                   217
Abc4                   530
.
.
.

Total                   867

The below code has some limitations like excluding the desired worksheet and the rows after row number 7 needs to be taken into consideration.

Function Test_It()
    For Each Sheet In ThisWorkbook.Sheets
        Debug.Print Sheet.Name & vbTab & CountMyRows(Sheet.Name)
    Next Sheet
End Function


Function CountMyRows(SName) As Long         '# where SName is the name of a sheet
    Dim rowCount As Long
    rowCount = Worksheets(SName).UsedRange.Rows.Count
    CountMyRows = rowCount
End Function

Thanks in advance
0
Comment
Question by:Milind Agarwal
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 total points
ID: 41812621
Please try this to see if this is what you are trying to achieve....

Sub CreateSummarySheet()
Dim sws As Worksheet, ws As Worksheet
Dim slr As Long
Dim Found As Boolean
Application.ScreenUpdating = False
On Error Resume Next
Set sws = Sheets("Summary")
sws.Cells.Clear
On Error GoTo 0
If sws Is Nothing Then
   Sheets.Add(Before:=Sheets(1)).Name = "Summary"
   Set sws = ActiveSheet
End If
With sws.Range("A1:B1")
   .Value = Array("Worksheet", "Count")
   .Font.Bold = True
   .Font.Size = 12
End With
For Each ws In Worksheets
   If ws.Name <> sws.Name And Not LCase(ws.Name) Like "table of content*" Then
      If ws.UsedRange.Rows.Count > 7 Then
         Found = True
         sws.Range("A" & Rows.Count).End(3)(2).Value = ws.Name
         sws.Range("B" & Rows.Count).End(3)(2).Value = ws.UsedRange.Rows.Count - 7
      End If
   End If
Next ws
If Found Then
   slr = sws.Cells(Rows.Count, 1).End(xlUp).Row
   With sws.Range("A" & slr + 1 & ":B" & slr + 1)
      .Value = Array("Total", Application.Sum(sws.Columns(2)))
      .Font.Bold = True
      .Font.Size = 12
   End With
   sws.Columns.AutoFit
   sws.Range("A1").CurrentRegion.Borders.Color = vbBlack
End If
Application.ScreenUpdating = True
End Sub

Open in new window

1
 

Author Closing Comment

by:Milind Agarwal
ID: 41812634
Worked like a dream
0
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41812730
You're welcome Milind! Glad to help.
1

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question