User-defined function and Application.Volatile

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
As far as I'm aware, Application.Volatile is rarely used, possibly frowned upon.

I have a udf that takes parameters:

Code:
Public Function MyFunction(ByRef a As Integer, ByRef b As Integer, ByRef c As Integer) As Integer

   MyFunction = a + b + c

End Function

As it is, this function will kick off if any of its three parameters changes.

However, parameter a is linked to another cell, say d.

If d changes, in order for my udf to update automatically, I suppose I would change the code to:

Code:
Public Function MyFunction(ByRef a As Integer, ByRef b As Integer, ByRef c As Integer) As Integer

    Application.Volatile

    MyFunction = a + b + c

End Function

My question is: is this a badly desgined spreadsheet?

Should parameter a NOT have to be linked to d?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
My question is: is this a badly designed spreadsheet?
If this UDF is used a few times in the workbook, probably not. If it is used hundreds of times or more, probably yes.

Also, if this workbook will be used in Excel 365 only, you can make use of the Lambda function to define a UDF without requiring VBA.
 
Upvote 0
However, parameter a is linked to another cell, say d.

If d changes, in order for my udf to update automatically, I suppose I would change the code to ...
If d changes, and hence a changes, doesn't your MyFunction update automatically (without Application.Volatile)?
 
Upvote 0
If this UDF is used a few times in the workbook, probably not. If it is used hundreds of times or more, probably yes.

Also, if this workbook will be used in Excel 365 only, you can make use of the Lambda function to define a UDF without requiring VBA.
Thanks, I'll try that.
 
Upvote 0
If d changes, and hence a changes, doesn't your MyFunction update automatically (without Application.Volatile)?
Yes, indeed it does.

However, my spreadsheet is more complicated, in that on another sheet, there are calculations (using VBA) which depend on the udf results.
 
Upvote 0
However, my spreadsheet is more complicated, in that on another sheet, there are calculations (using VBA) which depend on the udf results.
I don't see how that relates. If the inputs to your UDF change, your UDF will recalculate. Since they are all direct arguments, it should not be necessary to make it volatile.
 
Upvote 0
I don't see how that relates. If the inputs to your UDF change, your UDF will recalculate. Since they are all direct arguments, it should not be necessary to make it volatile.
I've figured it out.

My udf takes two arguments (say a and b) and uses a value (c) on another sheet.

So the udf only updates if either a or b is changed but if only c chnages, it won't update.
 
Upvote 0
Which is completely unlike the function you posted. ;)
Yes, it was my mistake.

Having said that, this has led me to find out something strange. Here is the link:

Code:
https://www.mrexcel.com/board/threads/step-into-udf.1262009/
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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