1

I am trying to simply count all cells in a column that have a background color. Any color it doesn´t matter which one. I have used a module using this code:

Function CountCcolor(range_data As Range) As Long
    Dim datax As Range
    For Each datax In range_data
        If datax.Interior.ColorIndex <> xlNone Then
            CountCcolor = CountCcolor + 1
        End If
    Next datax
End Function

This works good when I apply the formula for the first time. It no longer reacts, when I change fill in the sheet. How can I exceute this function, every time the users edit a cell?

2
  • Have a look here and here Commented Jul 18, 2016 at 7:34
  • Hi. thanks I saw that but I can´t get it working. I have only a module with that bit of code in it. In the Worksheet I call it like this =CountCcolor(B5:B18) which works fine. But if i fill new cells it doesn´t react. Sorry I´m new to VBA.. Commented Jul 18, 2016 at 7:44

2 Answers 2

0

You can refresh the entire workbook by using the following code, in the relevant Worksheet_Change event :

Private Sub Worksheet_Change(ByVal Target As Range)

Application.Calculation = xlCalculationManual
Application.Calculation = xlCalculationAutomatic

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for your answer. Thsi event is not called when I fill a cell. Only when I remove the fill.
0

There is no event which is triggered by filling a cell. Thus, I suggest the following worksheet macro

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Sh.Calculate
End Sub

in addition to your macro. Reasoning: after filling a range you will (eventually) select other cells on that sheet. At this moment, the sum is recalculated. It is not ideal as it's not updated while you're selecting the range, and it might be computationally intensive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.