80

How can I get a user-defined function to re-evaluate itself based on changed data in the spreadsheet?

I tried F9 and Shift+F9.

The only thing that seems to work is editing the cell with the function call and then pressing Enter.

Community
  • 1
  • 1
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91

9 Answers9

136

You should use Application.Volatile in the top of your function:

Function doubleMe(d)
    Application.Volatile
    doubleMe = d * 2
End Function

It will then reevaluate whenever the workbook changes (if your calculation is set to automatic).

Bond
  • 16,071
  • 6
  • 30
  • 53
vzczc
  • 9,270
  • 5
  • 52
  • 61
  • 20
    Thanks, just been bashing my head against a desk over this. Should point out though that in Excel 2010, you need to pass True to Application.Volatile, i.e. `Application.Volatile True`. – mdm Mar 31 '10 at 13:00
  • @mdm Thanks for the info on the True!! Needed this also for a custom worksheet function in Excel 2013!! – timbram Dec 11 '15 at 18:28
  • 2
    this works when I change the value of a cell, but I have a function that sums cells based on fill color and this doesn't update the function when I change the cell's fill color, I have to retype the value. can I have the worksheet live update as I change Fill Color? – MilkyTech Jun 28 '16 at 00:25
  • @ChrisL Did you find a solution for your use case with the calculation based on fill color? Appreciate your solution. – smartini May 30 '20 at 16:02
  • @smartini I never continued looking for a solution. I wasn't using my fill color function often enough for it to matter. I was hoping for a quick answer to my comment 4 years ago but it never came. Maybe best to start a new question. If you do, hit me up with a comment here so I can follow it. – MilkyTech May 30 '20 at 16:24
44

Some more information on the F9 keyboard shortcuts for calculation in Excel

  • F9 Recalculates all worksheets in all open workbooks
  • Shift+ F9 Recalculates the active worksheet
  • Ctrl+Alt+ F9 Recalculates all worksheets in all open workbooks (Full recalculation)
  • Shift + Ctrl+Alt+ F9 Rebuilds the dependency tree and does a full recalculation
Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Robert Mearns
  • 11,796
  • 3
  • 38
  • 42
18

Okay, found this one myself. You can use Ctrl+Alt+F9 to accomplish this.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
12

If you include ALL references to the spreadsheet data in the UDF parameter list, Excel will recalculate your function whenever the referenced data changes:

Public Function doubleMe(d As Variant)
    doubleMe = d * 2
End Function

You can also use Application.Volatile, but this has the disadvantage of making your UDF always recalculate - even when it does not need to because the referenced data has not changed.

Public Function doubleMe()
    Application.Volatile
    doubleMe = Worksheets("Fred").Range("A1") * 2
End Function
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
1

To switch to Automatic:

Application.Calculation = xlCalculationAutomatic    

To switch to Manual:

Application.Calculation = xlCalculationManual    
ayman
  • 29
  • 4
1

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
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
MfJ
  • 11
  • 1
1

The Application.Volatile doesn't work for recalculating a formula with my own function inside. I use the following function: Application.CalculateFull

aniski
  • 1,263
  • 1
  • 16
  • 31
alex303411
  • 21
  • 1
1

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
Robert
  • 7,394
  • 40
  • 45
  • 64
Gene Sorg
  • 11
  • 1
0
Public Sub UpdateMyFunctions()
    Dim myRange As Range
    Dim rng As Range

    'Considering The Functions are in Range A1:B10
    Set myRange = ActiveSheet.Range("A1:B10")

    For Each rng In myRange
        rng.Formula = rng.Formula
    Next
End Sub
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38