mykulpasskwa
Board Regular
- Joined
- Mar 20, 2018
- Messages
- 66
I have a range starting at B4 with term codes grouped together year and I'm trying to add the values that start with the same 4 digit year code. So 2019 should be 28,000; 2020 should be 28,500; and 2021 should be 6 (test sample). The idea is the user selects a cell and VBA will add that year together. If they select another year, the result is for that year. The issue I'm having right now is that it's adding 201910 and 201930, but it's skipping 201950, and it's only totaling to 25000 instead of 28000. I seem to have something out of order or I'm not using something correctly. Any ideas?
VBA Code:
Sub Sum_by_Month_and_Year()
'declare variables
Dim ws As Worksheet
Dim output As Range
Dim Lresult As String
Dim yearvalue As String
Dim counter As Long
Dim f As Integer
Dim l As Integer 'lowercase L
Dim X As Integer
Set ws = Workbooks("Macro OG Recon Calculator - SANDBOX.xlsm").ActiveSheet
Set output = ws.Range("F150")
counter = 0
'sum value by year
f = ActiveCell.Row
l = Range("B4").End(xlDown).Row
For X = f To l
Lresult = Left(ActiveCell.Value, 4)
yearvalue = Left(ActiveCell.Offset(1, 0).Value, 4)
If Lresult = yearvalue Then
counter = counter + ws.Range("F" & X)
ActiveCell.Offset(1, 0).Select
End If
Next X
output = MsgBox(counter)
ws.Range("F150").Value = ""
End Sub