Refresh Excel VBA Function Results

后端 未结 9 911
悲&欢浪女
悲&欢浪女 2020-11-27 06:01

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

I tried F9 and Shift

相关标签:
9条回答
  • 2020-11-27 06:05

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

    0 讨论(0)
  • 2020-11-27 06:07

    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
    
    0 讨论(0)
  • 2020-11-27 06:11

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

    0 讨论(0)
  • 2020-11-27 06:11

    To switch to Automatic:

    Application.Calculation = xlCalculationAutomatic    
    

    To switch to Manual:

    Application.Calculation = xlCalculationManual    
    
    0 讨论(0)
  • 2020-11-27 06:16

    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
    
    0 讨论(0)
  • 2020-11-27 06:18

    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
    0 讨论(0)
提交回复
热议问题