Add occurrences of same month data

Drawleeh

New Member
Joined
Sep 2, 2021
Messages
34
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hello, I have a spreadsheet that looks something like this.

1638284357213.png

Id like to be able to add together the date occurrences by month in VBA (so add all the dates from January to show how many January entries there have been). But also add them together based by their category. So the output from above would be something like this but from a much larger list.

1638286194852.png
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
While VBA is possible, here are some other possibilities:

Book1
ABC
1Time ChangedUserCategory
21/1/2021UserC285_Repayment_Overpayment
31/2/2021UserC285_Repayment_Overpayment
42/2/2021UserC2001_Voluntary_Underpayment
53/3/2021UserC2001_Voluntary_Underpayment
63/3/2021UserC2001_Voluntary_Underpayment
74/4/2021UserC285_Repayment_Overpayment
85/4/2021UserC285_Repayment_Overpayment
96/5/2021UserC2001_Voluntary_Underpayment
106/5/2021UserC2001_Voluntary_Underpayment
117/5/2021UserC285_Repayment_Overpayment
12
13
14
15C285_Repayment_OverpaymentC2001_Voluntary_Underpayment
16January20
17February01
18March02
19April10
20May10
Sheet14
Cell Formulas
RangeFormula
B16:C20B16=COUNTIFS($A$2:$A$11,">="&$A16,$A$2:$A$11,"<="&EOMONTH($A16,0),$C$2:$C$11,B$15)


This formula will count them easily. Note that the values in A16:A20 are actual dates, just formatted to look like a month.

Or you can just use a pivot table:

Book1
ABCD
1
2
3Count of CategoryColumn Labels
4Row LabelsC2001_Voluntary_UnderpaymentC285_Repayment_OverpaymentGrand Total
5Jan22
6Feb11
7Mar22
8Apr11
9May11
10Jun22
11Jul11
12Grand Total5510
Sheet15


To do that, just select your data, Click "Insert Pivot Table", and from the PivotTable Fields box, drag "Time Changed" to the Rows box, "Category" to the Columns and Values boxes.
 
Upvote 0
While Eric is correct, and you could add Power Query to that list of better solutions....

Since you asked, here's some VBA.

Book1
ABCDEFG
1Time ChangedUserCategory
2------------------------------------C285C2001
31/1/2021UserC285January20
41/2/2021UserC285February01
52/2/2021UserC2001March02
63/3/2021UserC2001April10
73/3/2021UserC2001May10
84/4/2021UserC285June02
95/4/2021UserC285July10
106/5/2021UserC2001
116/5/2021UserC2001
127/5/2021UserC285
Sheet1


VBA Code:
Sub DRW()
Dim AR() As Variant:    AR = Range("A3:C" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim Cat As Object:      Set Cat = CreateObject("Scripting.Dictionary")
Dim CH As Object:       Set CH = CreateObject("Scripting.Dictionary")
Dim MO As Object:       Set MO = CreateObject("Scripting.Dictionary")
Dim bCol As Integer:    bCol = 6
Dim Res() As Variant
Dim cId As String
Dim Mth As String

For i = 1 To UBound(AR)
    cId = AR(i, 3)
    Mth = Format(AR(i, 1), "MMMM")
    If Not Cat.exists(cId) Then
        Cat.Add cId, True
        CH.Add cId, CreateObject("Scripting.Dictionary")
    End If
    If Not MO.exists(Mth) Then MO.Add Mth, True
Next i

For i = 1 To UBound(AR)
    cId = AR(i, 3)
    Mth = Format(AR(i, 1), "MMMM")
    
    If Not CH(cId).exists(Mth) Then
        CH(cId).Add Mth, 1
    Else
        CH(cId).Item(Mth) = CH(cId).Item(Mth) + 1
    End If
Next i

FormatRange Range("E3"), MO, True
FormatRange Range("F2"), Cat, False

For Each k In Cat.keys
    For j = 0 To MO.Count - 1
        If CH(k).exists(MO.keys()(j)) Then
            Cells(j + 3, bCol) = CH(k).Item(MO.keys()(j))
        Else
            Cells(j + 3, bCol) = 0
        End If
    Next j
    bCol = bCol + 1
Next k
End Sub

Sub FormatRange(r As Range, SD As Object, b As Boolean)
If b Then
    With r.Resize(SD.Count)
        .Value = Application.Transpose(SD.keys)
        .Font.Bold = True
    End With
Else
    With r.Resize(, SD.Count)
        .Value = SD.keys
        .Font.Bold = True
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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