Microsoft Office Tutorials and References
In Depth Information
Generating Random Numbers
Controlling function recalculation
When you use a custom function in a worksheet formula, when is it recalculated?
Custom functions behave like Excel’s built-in worksheet functions. Normally, a custom function is
recalculated only when it needs to be recalculated — that is, when you modify any of a
function’s arguments — but you can force functions to recalculate more frequently. Adding the
following statement to a Function procedure makes the function recalculate whenever the
workbook is recalculated:
Application.Volatile True
The Volatile method of the Application object has one argument (either True or False ).
Marking a Function procedure as “volatile” forces the function to be calculated whenever
calculation occurs in any cell in the worksheet.
For example, the custom STATICRAND function presented in this chapter can be changed to
emulate the Excel RAND() function by using the Volatile method, as follows:
‘ Returns a random number that
‘ changes when the sheet is recalculated
Application.Volatile True
End Function
Using the False argument of the Volatile method causes the function to be recalculated
only when one or more of its arguments change (if a function has no arguments, this method
has no effect). By default, all functions work as if they include an Application.Volatile
False statement.
If you use this function, you’ll find that it is not recalculated when the worksheet is calculated. In
other words, the function is not a volatile function. (For more information about controlling
recalculation, see the nearby sidebar, “Controlling function recalculation.” You can make the function
volatile by adding the following statement:
Application.Volatile True
After doing so, the DRAWONE function displays a new random cell value whenever the sheet is
Search JabSto ::

Custom Search