Summing rows above blank cell untill next blank cell and excluding rows from sum that do not meet criteria from another cell

58 views Asked by At

enter image description hereI have a dataset which is separated by a blank rows. I need to sum values in column C for all the rows above blank cell and exclude cell value from sum total if criteria in column A, B & T are met. And return the sum total value in the blank cell.

For example: If column A = M, Column B = Buy and Column E = DAC then the Qty in column D for that row should not be considered while summing.

As you see in the image for Client A, row 1 is not considered in the total highlighted in yellow. Similarly for Client C, row 25 was not included in the total.

I found below code that worked beatifully to sum up values for all the rows above blank cell until next blank cell. But I am unable to add the criteria’s. I tried to use the Sumif/sumifs function but did not get the result. Any help will be appreciated.

Sub SumValues()
 
Dim FirstCell As Range
Set FirstCell = Range(“D2”)
 
Dim VeryLastCell As Range
Set VeryLastCell = Cells(Rows.Count, “D”).End(xlUp)

Do
    Dim LastCell As Range
    
    If FirstCell.Offset(1) = vbNullString Then
        Set LastCell = FirstCell
    Else
        Set Lastcell = FirstCell.End(xlDown)
    End if

    With LastCell.Offset(1, 0)
            .Value = Application.WorksheetFunction.Sum(Range(FirstCell, LastCell))
            .Interior.Color = RGB(253, 234, 123)
            .Font.Color = vbBlack
            .Font.FontStyle = “bold”
            .Font.Underline = xlUnderlineStyleSingle
    End With

    Set FirstCell = LastCell.Offset(2, 0)

Loop While FirstCell.Row < VeryLastCell.Row

End Sub
0

There are 0 answers