How to make my VBA function volatile?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hello,

I have written a custom function in VBA that includes a random component, with VBAs RND function, but unfortunately it doesn't behave as a volatile function when I use it in my worksheet. For example, it doesn't automatically recalculate when I hit the delete key on a blank cell.

The best I have come up with is to use the following formula in my spreadsheet:

"= MyFunction() + 0*RAND()"

Is there any way to change the VBA code so that the function is volatile when I use it in a worksheet?

Thanks for any help,
Kelvin
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Silly me, I just realized I can call the RAND() function from the worksheet into the VBA function, instead of using VBA's RND function within the code itself, so I now have:

"= MyFunction(RAND())"

This makes it perfectly volatile! :)
 
Upvote 0
Another way:
Code:
Function MyFunction() as Whatever
    Application.Volatile True
    ' ...
 
Upvote 0
Just tested, and it works perfectly, even when the VBA function is used within other VBA functions, all the dependent VBA functions automatically become volatile also! :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top