Mar 21 2023 07:28 PM
I have defined a function that calculates the sum of cells depending on their color. This function works perfectly but refuses to refresh, it doesn't refresh when I click calculate sheet, it doesn't refresh when I calculate cell. The only way is to select it, edit the formula then click enter. I'd like it to refresh automatically, here is the code for the function.
Function SumColoredCells(CC As Range, RR As Range) As Double
Dim X As Double
Dim Y As Long
Y = CC.Interior.ColorIndex
For Each I In RR
If I.Interior.ColorIndex = Y Then
X = X + I.Value
End If
Next I
SumColoredCells = X
End Function
Mar 22 2023 12:27 AM
SolutionI want refresh calculation in this VBA code.
you can try adding the following line of code at the beginning of your function:
Application.Volatile
This will make sure that Excel recalculates the function every time there is a change in the worksheet. If this does not work, you can try adding a dummy argument to your function and changing its value every time you want to recalculate the function.
Here’s an example:
Application.Volatile
Dim X As Double
Dim Y As Long
Y = CC.Interior.ColorIndex
For Each I In RR
If I.Interior.ColorIndex = Y Then
X = X + I.Value
End If
Next I
SumColoredCells = X
End Function```
Hope I could help you with this information.
I know I don't know anything (Socrates)
Mar 22 2023 02:20 PM
Mar 22 2023 12:27 AM
SolutionI want refresh calculation in this VBA code.
you can try adding the following line of code at the beginning of your function:
Application.Volatile
This will make sure that Excel recalculates the function every time there is a change in the worksheet. If this does not work, you can try adding a dummy argument to your function and changing its value every time you want to recalculate the function.
Here’s an example:
Application.Volatile
Dim X As Double
Dim Y As Long
Y = CC.Interior.ColorIndex
For Each I In RR
If I.Interior.ColorIndex = Y Then
X = X + I.Value
End If
Next I
SumColoredCells = X
End Function```
Hope I could help you with this information.
I know I don't know anything (Socrates)