Re: How to add values of a column only if certain condition is satisfied
Here you go:
Code
Sub SumValues()
Dim lWrkRow As Long
Dim lLastRow As Long
Dim iRefCol As Integer
Dim iSumCol As Integer
Dim dTempSum As Double
'Assumes the data starts on row 1
lWrkRow = 1
'These should really be based on named ranges rather
'than harcoding the locations.
iRefCol = 7
iSumCol = 8
dTempSum = 0
lLastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row + 1
'Again assumes data starts on the first row
For lWrkRow = 1 To lLastRow
If Cells(lWrkRow, iRefCol) = 9 Then
dTempSum = dTempSum + Cells(lWrkRow, iSumCol)
Else
Cells(lWrkRow, iSumCol) = "Sum = " & dTempSum
dTempSum = 0
End If
Next lWrkRow
End Sub
Display More