Refresh Excel VBA Function Results

后端 未结 9 912
悲&欢浪女
悲&欢浪女 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:18

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

    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).

    0 讨论(0)
  • 2020-11-27 06:28
    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
    
    0 讨论(0)
提交回复
热议问题