Hello BigMehello,
kindly help me please, how to sum data in dynamic multiple range? thank you.
View attachment 1085
Private Sub Worksheet_Change(ByVal Target As Range)
Dim totalCell As Range
Dim totalRange As Range
Dim cell As Range
Dim sumTotal As Double
Dim lastRow, startRowTemp As Long
Set totalRange = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
startRowTemp = 2
If Not Intersect(Target, totalRange) Is Nothing Then
For Each cell In totalRange
If InStr(1, cell.Value, "Total") > 0 Then
lastRow = cell.Row
sumTotal = Application.WorksheetFunction.Sum(Range("C" & startRowTemp & ":C" & lastRow - 1))
Set totalCell = Cells(lastRow, 3)
totalCell.Value = sumTotal
startRowTemp = lastRow + 1
End If
Next cell
End If
End Sub
Dear BigMeDear Lutfor,
thank you for your response, is this code can run if i copied to a module? because i want to merge & modify with other macros, thank you
regards,
bigMe
Sub SumDynamicMultipleRange()
Dim totalCell As Range
Dim totalRange As Range
Dim cell As Range
Dim sumTotal As Double
Dim lastRow, startRowTemp As Long
Set totalRange = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
startRowTemp = 2
For Each cell In totalRange
If InStr(1, cell.Value, "Total") > 0 Then
lastRow = cell.Row
sumTotal = Application.WorksheetFunction.Sum(Range("C" & startRowTemp & ":C" & lastRow - 1))
Set totalCell = Cells(lastRow, 3)
totalCell.Value = sumTotal
startRowTemp = lastRow + 1
End If
Next cell
End Sub