(Solved) – VBA If find the header is equal then sum the column below

  • by

I want to build a dynamic reporting, and for that, if the heaser is equal to a specific text then sum the entire column below the header.

Sub FindIfSumColumn()

Dim LastRow As Long
Dim rgFound As Range
Dim mFound As Range
Dim bd As Worksheet: Set bd = Sheets("BDD")
Dim dt As Worksheet: Set dt = Sheets("DICT")

LastCol = bd.Cells(1, Columns.Count).End(xlToLeft).Column

Set mFound = dt.Range("B2")
Set rgFound = bd.Range("A1:XFD" & LastCol).Find(What:=mFound, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns)
 If rgFound Is Nothing Then
    MsgBox "Nothing"
    LastRow = rgFound.Cells(Rows.Count, 1).End(xlUp).Row
    dt.Range("B4") = Application.WorksheetFunction.Sum(LastRow)
 End If
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *