I have a macro that runs when any cell is changed, and I want to sum only the shown cells, so if someone filters the sheet the macro will run and sum just the visible cells.
I found the SpecialCells(xlCellTypeVisible) function but I can’t manage to make it work.
My idea of the code is something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rcells As Range
Dim sum as Double
sum = 0
For each Rcells In Range("A5:A65536").SpecialCells(xlCellTypeVisible)
sum = sum + Rcells.Value
Next Rcells
Sheets("aSheet").Range("B1").Value = sum
End Sub
I managed to sum just the visible cells but it does not run automatically.
Why is it not working?
The two key two steps from my article Trapping a change to a filtered list with VBA which includes detailed instructions and a sample fle:
SUBTOTALformula inA1pointing back to the range being filtered on the main sheet (i.e. the range you want to capture the filter on).Worksheet_Calculate()Event is added to the dummy WorkSheet, this Event fires when theSUBTOTALformula updates when the filter is changed on your main sheet.