I have some VBA in Excel that counts if a range of cells contains a colour from a reference cell, which works well. But changing the colour of any cell in the range does not update the total counted.
Ideally, I want the code to update the number of coloured cells that match the reference, and when the cell colours are changed, to update the sum counted.
I have tried adding an Else statement in the If statement so that if a cell is 'blank' or had its colour 'cleared' using the paint pot button for filling cells, it would deduct a number, but this failed to work.
Sub Colourif()
End Sub
Function CountColorIf(rSample As Range, rArea As Range) As Long
Dim rAreaCell As Range
Dim lMatchColor As Long
Dim lCounter As Long
lMatchColor = rSample.Interior.Color
For Each rAreaCell In rArea
If rAreaCell.Interior.Color = lMatchColor Then
lCounter = lCounter + 1
End If
Next rAreaCell
CountColorIf = lCounter
End Function
If I change a cell in the range fill using the paint pot icon to say 'clear' it does not change the count of cells filled with the reference colour.
I would be extremely grateful for some advice on how to ensure this code automatically updates the sum when cell fills are changed from the reference colour.
Sub ColorIf
? It has no executable statements. In any event, changing a cell color does not trigger a worksheet calculation, which might be your issue. – John Coleman