Vba Loop through all tabs and count

ffionnah

Board Regular
Joined
Jun 12, 2018
Messages
61
Hi all!
I am semi familiar with macros but I’m having difficulties knowing where to start for a request. The purpose is to loop through all tabs within a workbook and count the rows (Count count text via column “B” that will not be empty unless the data comes to an end) based on a date entered into column “AB”. I would like for this to either display in a message box or could be consolidated to a new sheet.

such as:
5/26/2020 - 48
5/27/2020 - 120
5/28/2020 - 87

Is this possible? The counting/ sum per date, across all tabs is what I am stumbling with most.

Thank you for all of your insights!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Put the dates in column AB on sheet1. As it's shown in the following.
The results will be in column AC.

varios 28may2020.xlsm
ABAC
1
226/05/20203
327/05/20202
428/05/20201
Sheet1


VBA Code:
Sub CountDates()
  Dim sh As Worksheet, c As Range, nSum As Double
  For Each sh In Sheets
    For Each c In Sheets("Sheet1").Range("AB2", Sheets("Sheet1").Range("AB" & Rows.Count).End(3))
      nSum = WorksheetFunction.CountIf(sh.Range("B2", sh.Range("B" & Rows.Count).End(3)), c)
      c.Offset(, 1).Value = c.Offset(, 1).Value + nSum
    Next
  Next
End Sub
 
Upvote 0
Good morning,
Thank you for the reply! I tried your code and, unfortunately, it didn't count the rows that have a date in column "AB".
I added Sheets.Add and ActiveSheet.Name = "Counts" and a sort for each tab because there may be rows that are skipped to your code and it produces two "0" in column AC of the "Counts" sheet.


Code:
Sub CountDates()
  Dim sh As Worksheet, c As Range, nSum As Double
  Sheets.Add
  ActiveSheet.Name = "Audit Counts"
  
  
    ActiveSheet.Range("a1:cw1").Select
   Selection.Copy
   On Error Resume Next
   Application.ScreenUpdating = False
   
  For Each sh In Sheets
      sh.Columns("A:cw").Sort Key1:=sh.Columns("AB"), Order1:=xlDescending
   Next sh

   ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
   Application.ScreenUpdating = True
    
   For Each sh In Sheets
   
    For Each c In Sheets("Audit Counts").Range("AB2", Sheets("Audit Counts").Range("AB" & Rows.Count).End(3))
      nSum = WorksheetFunction.CountIf(sh.Range("B2", sh.Range("B" & Rows.Count).End(3)), c)
      c.Offset(, 1).Value = c.Offset(, 1).Value + nSum
    Next
  Next
End Sub





sample data:

(skipping columns just to display data)

Column B will always have data entered. Column AB is what I need for the counts to be grouped by. I would like for the "Counts" sheet to display:
5/27/2020 2
5/28/2020 1
5/29/2020 1

ABCABAC
1234JOHNDOE5/28/2020
MINNIEMOUSE
1789SANDRADOE5/29/2020
2489MICKEYMOUSE5/27/2020
DONALDDUCK5/27/2020

Is this possible?

Thank you for all of your help!
 
Upvote 0
Sorry i misunderstood.
So you want the results in the "Audit Counts" sheet, the list of dates in column A and the count in column B.
It is not necessary to create the "Audit Counts" sheet in every run, just create the sheet and run the macro.

This is what I understand.
Before running the macro (Audit Counts sheet):
varios 28may2020.xlsm
AB
1DateCount
2
3
4
5
Audit Counts


I have 3 sheets (sheet1, sheet2 and sheet3):
varios 28may2020.xlsm
AB
226/05/2020
327/05/2020
428/05/2020
529/05/2020
630/05/2020
Sheet1

varios 28may2020.xlsm
AB
226/05/2020
327/05/2020
Sheet2

varios 28may2020.xlsm
AB
226/05/2020
327/05/2020
428/05/2020
Sheet3


After running the macro:
varios 28may2020.xlsm
AB
1DateCount
226/05/20203
327/05/20203
428/05/20202
529/05/20201
630/05/20201
Audit Counts


If the above is correct, try this
VBA Code:
Sub CountDates()
  Dim sh As Worksheet, sh1 As Worksheet
  Dim a As Variant, dic As Object, i As Long
  
  Set sh1 = Sheets("Audit Counts")
  Set dic = CreateObject("Scripting.Dictionary")
  
  sh1.Rows("2:" & Rows.Count).ClearContents
  
  For Each sh In Sheets
    a = sh.Range("AB2", sh.Range("AB" & Rows.Count).End(3)).Value2
    For i = 1 To UBound(a)
      If a(i, 1) <> "" And IsNumeric(a(i, 1)) Then
        dic(a(i, 1)) = dic(a(i, 1)) + 1
      End If
    Next
    Erase a
  Next
  sh1.Range("A2").Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.keys, dic.items))
End Sub
 
Upvote 0
Hi!
Not a problem. Thank you for your quick reply. Unfortunately, our firewalls do not allow for creating dictionaries. Is there a way around this? I tried to google a bit but I didn't find much that provided clarity!
 
Upvote 0
Add “Microsoft Scripting Runtime”using Tools->References from the VB menu

And try this

Rich (BB code):
Sub CountDates()
  Dim sh As Worksheet, sh1 As Worksheet
  Dim a As Variant, dic As Dictionary, i As Long
  
  Set sh1 = Sheets("Audit Counts")
  Set dic = CreateObject("Scripting.Dictionary")
  
  sh1.Rows("2:" & Rows.Count).ClearContents
  
  For Each sh In Sheets
    a = sh.Range("AB2", sh.Range("AB" & Rows.Count).End(3)).Value2
    For i = 1 To UBound(a)
      If a(i, 1) <> "" And IsNumeric(a(i, 1)) Then
        dic(a(i, 1)) = dic(a(i, 1)) + 1
      End If
    Next
    Erase a
  Next
  sh1.Range("A2").Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.keys, dic.items))
End Sub

Before presenting another alternative. Can you answer, if the images that I presented, is what you really want?
 
Last edited:
Upvote 0
Hi,
Thank you for all of your help! This code did work. I had to have our IT department whitelist the line item. Thanks again!
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,654
Messages
6,126,048
Members
449,282
Latest member
Glatortue

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