VBA program to sum minutes per month

cocopoo

New Member
Joined
Mar 29, 2022
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
Hi I would like to write a program that sums up minutes per month and displays a pop up for each month's sum. How would I go about that? Thank you
MonthMinutes
Jan17
Jan39
Jan36
Jan65
Feb52
Feb40
Mar100
Mar98
Mar53
Apr23
Apr27
Apr99
May20
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Assuming your data begins in cell A1.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range:         Set r = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Dim Itm As String:      If Target.Column = 2 Then Itm = Target.Offset(, -1).Value Else Itm = Target.Value

If Not Intersect(Target, r) Is Nothing Then

    Dim AR() As Variant:    AR = r.Value2
    
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(AR)
            If Not .Exists(AR(i, 1)) Then
                .Add AR(i, 1), AR(i, 2)
            Else
                .Item(AR(i, 1)) = .Item(AR(i, 1)) + AR(i, 2)
            End If
        Next i
        
        MsgBox .Item(Itm)
    End With
    
End If
End Sub
 
Upvote 0
thank you would there also be a simpler way to do it with a nested loop?
 
Upvote 0
Here's a no-loop way.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LR As Long:     LR = Range("A" & Rows.Count).End(xlUp).Row
Dim r As Range:     Set r = Range("A2:B" & LR)

If Not Intersect(Target, r) Is Nothing Then
    Dim a As Range:     Set a = Range("A2:A" & LR)
    Dim b As Range:     Set b = Range("B2:B" & LR)
    
    MsgBox Evaluate(Replace(Replace("=LET(u,UNIQUE(@),s,SUMIF(@,u,^),c,CHOOSE({1,2},u,s),ca,INDEX(c,,1),cb,INDEX(c,,2),TEXTJOIN(CHAR(10),1,ca & "" - "" & cb))", "@", a.Address), "^", b.Address))
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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