Solved

A macro to Count the number of rows across all worksheets

Posted on 2016-09-23
3
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 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 31

Expert Comment

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

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

630 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