Sub SumByCondition()
' Assign the worksheet to a variable
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
' Initialize the sum variable
Dim SumQ1 As Double
Dim SumQ2 As Double
' Define the last row of data
Dim lastRow As Long
lastRow = ws.Cells(Rows.Count, 2).End(xlUp).Row
' Loop through the data in column B
For i = 1 To lastRow
' Check if the value in column B equals 'Q1'. If so, add the corresponding value in column A to the sum
If ws.Cells(i, 2).Value = "Q1" Then
SumQ1 = SumQ1 + ws.Cells(i, 1).Value
' Check if the value in column B equals 'Q2'. If so, add the corresponding value in column A to the sum
ElseIf ws.Cells(i, 2).Value = "Q2" Then
SumQ2 = SumQ2 + ws.Cells(i, 1).Value
End If
Next i
' Output the sums to the debug window
[debug.print](/vba/debug-print-immediate-window) "Q1 Sum: " & SumQ1
Debug.Print "Q2 Sum: " & SumQ2
End Sub