eggyjla
Active Member
- Joined
- Dec 16, 2004
- Messages
- 346
When creating a UDF, is it preferred to make it volatile or NOT. What are the pros/cons of volatile functions. I have a couple UDFs that each are similar. One updates anytime any of the variables changes and the other requires Ctrl+Alt+F9 or Application.Volatile added in the function in order to update.
The following will update anytime Var1 or Var2 are modifed.
In this function, the values are not updated as Var1 or Var2 are modifed. Application.Volatile or Ctrl+Alt+F9 are required to update the value.
The following will update anytime Var1 or Var2 are modifed.
Code:
Function Var4(Var1 As Double, Var2 As Double) As Variant
Dim WF As WorksheetFunction
Set WF = WorksheetFunction
Dim Rng2 As Range
Dim d As Double
d = 1
On Error Resume Next
d = WF.VLookup(Var1, Range("Rng2"), 2, 0)
Var4= (Var1* 1.38 / d + Var2) / (Var1* 1.38 / (d * 0.8) + Var2
End Function
In this function, the values are not updated as Var1 or Var2 are modifed. Application.Volatile or Ctrl+Alt+F9 are required to update the value.
Code:
Function Var1Var2(Var1 As Double, Optional Var2) As Variant
Dim WF As WorksheetFunction
Set WF = WorksheetFunction
Dim Rng As Range
If IsMissing(Var2) Then Var2= 1
Var2= WF.VLookup(Var1, Range("Rng"), 2)
Var1Var2= Var1 * 1.38 / Var2
End Function