Problem with VBA - function needs to be constantly refreshed for proper values

MZ1

New Member
Joined
Jan 12, 2022
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Dear all,
I have an issue with the code below.
VBA Code:
    Function CountColor(CellsRange As Range, ReferenceCell As Range) As Integer
       Dim indRefColor As Long
       Dim Format As Range
       Dim CountColor2 As Integer
       
       Application.Volatile
       CountColor = 0
       indRefColor = Evaluate("cfcolour(" & ReferenceCell.Address & ")")
       For Each cell In CellsRange
          If Evaluate("CFColour(" & cell.Address & ")") = indRefColor Then
             CountColor = CountColor + 1
          End If
       Next
    End Function

    Function CFColour(Cl As Range) As Double
       CFColour = Cl.DisplayFormat.Interior.Color
    End Function

The main idea behind this code is to calculate the number of cells which have the same background colour as the particular cell.
the code is working, but when you change the sheet you have to manually refresh it to have proper values.

Even if you save an excel file and get to a different sheet you get bad values. It will be very helpful if someone can tell me how to refresh this code when the excel sheet is changed or how to change the code so changes in one sheet didn't affect values on the other sheets.

Many thanks for your help.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The probelm is that changing the formatting doesn't triggerthe worksheet change event and so doesn't trigger a recalculation. You can force a recalcualtion by putting this code in the workbook workhseet selection change which means it will update as soon as you select another cell.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Calculate
End Sub
 
Upvote 0
Solution
Thank you very much. Your solution works!!!
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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