Solved

A macro to Count the number of rows across all worksheets

Posted on 2016-09-23
3
62 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 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
Comment Utility
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
Comment Utility
Worked like a dream
0
 
LVL 28

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now