VBA Function Not Working

Ken Hudson

New Member
Joined
Sep 10, 2007
Messages
46
I had previously used the following function to count the number of cells with a certain interior cell color. It does not seem to work now.
Can someone tell me what the problem might be?

Formula in cell A11:

=CountRedBYCOLOR(A1:A10,3,TRUE)

Code:
Function CountRedByColor(InRange As Range, WhatColorIndex As Integer, Optional OfText As Boolean = False) As Double

Dim Rng As Range
Dim OK As Boolean
Dim RedCt As Integer
RedCt = 0

Application.Volatile True
For Each Rng In InRange.Cells
    If OfText = True Then
        If Rng.Interior.ColorIndex = WhatColorIndex Then
            RedCt = RedCt + 1
        End If
    End If
Next Rng
CountRedByColor = RedCt

End Function
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Can you be more specific as to what you mean by " ... does not seem to work now"?
 
Upvote 0
Thanks for the reply.
I realized that the function will work as long as I change a value in one of the cells.
If I change only the background color, the function is not called unless/until I either change a value in the cell or hit enter in the formula bar.
Does that make sense?
Assuming there is no fix for this, I assume that I then need to write code in a module with an "update colors" button?
 
Upvote 0
A UDF will calculate only when the sheet is calculated or you select the cell it is in, press the F2 key and then press enter. Changing the color fill in a cell does not cause a calculation.
 
Upvote 0

Forum statistics

Threads
1,215,764
Messages
6,126,749
Members
449,335
Latest member
Tanne

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top