Hi All,
I recently created a macro (with a little help from some forums) that was designed to sum a row of cells IF the cell formatting used a particular colour in the background. It worked a treat, and this is what it looked like
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
It does what I want it to do, except when I use 'undo'. For instance, I change one of the cells to my special background colour (blue) - the sum changes as planned, but then if I click undo, the background changes back to the old colour (grey) but instead of the sum changing again, it keeps the new total. i.e the sum of blue cells and one grey cell. I want it to only show me the totals of my blue cells.
Can anyone suggest how I ought to tweak the above code so that it will always show the correct total?
Bookmarks