How can I get a user-defined function to re-evaluate itself based on changed data in the spreadsheet?
I tried F9 and Shift
The Application.Volatile
doesn't work for recalculating a formula with my own function inside. I use the following function:
Application.CalculateFull
This refreshes the calculation better than Range(A:B).Calculate
:
Public Sub UpdateMyFunctions()
Dim myRange As Range
Dim rng As Range
' Assume the functions are in this range A1:B10.
Set myRange = ActiveSheet.Range("A1:B10")
For Each rng In myRange
rng.Formula = rng.Formula
Next
End Sub
Okay, found this one myself. You can use Ctrl+Alt+F9 to accomplish this.
To switch to Automatic:
Application.Calculation = xlCalculationAutomatic
To switch to Manual:
Application.Calculation = xlCalculationManual
I found it best to only update the calculation when a specific cell is changed. Here is an example VBA code to place in the "Worksheet" "Change" event:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F3")) Is Nothing Then
Application.CalculateFull
End If
End Sub
Some more information on the F9 keyboard shortcuts for calculation in Excel