Auto update a UDF value

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a UDF that depends on the value on another sheet:

Code:
Option Explicit

Function MyFn(val As Double) As Double
    
    If Sheet1.Cells(1, 1).Value = 1 Then
    
        MyFn = val + 10
    
    Else
    
        MyFn = val + 20
    
    End If
    
End Function

So in Sheet2, cell B2, I type:

Code:
myfn(10)

which gives me a value of 20, IF the value in cell A1 of Sheet1 is 1.

The problem is if I change the value in cell A1 of Sheet1, the value in Sheet2, cell B2 is NOT automatically changed.

I have to physically go to cell B2 of Sheet2 and press F2.

What can I do so that by changing the value in cell A1 of Sheet1, the value in cell B2 of Sheet2 AUTOMATICALLY updates?

Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this:-
Code:
Function MyFn(val As Double) As Double
[COLOR=#FF0000] Application.Volatile[/COLOR]
    If Sheet1.Cells(1, 1).Value = 1 Then
      MyFn = val + 10
    Else
       MyFn = val + 20
    End If
End Function
 
Upvote 0
Thanks.

However, according to Chip Pearson:

Rich (BB code):
Writing Your Own Functions In VBA

"This has the drawback, however, that the function is recalculated even if it doesn't need to be recalculated, which can cause a performance problem. In general, you shouldn't use Application.Volatile but instead design your UDF to accept as inputs everything it needs to properly caclulate the result."

Is it a reasonable compromise?
 
Last edited:
Upvote 0
A much better alternative is to pass all relevant ranges as arguments to the UDF.
 
Upvote 0
That's what Chip said!

Can you give me pointers as to how might I achieve that?

For example:

Code:
Option Explicit
Function MyFn(firstval As Double, val As Double) As Double
    
    If firstval = 1 Then
    
        MyFn = val + 10
    
    Else
    
        MyFn = val + 20
    
    End If
    
End Function

this now doesn't take the value in cell A1 of Sheet1 into consideration.
 
Last edited:
Upvote 0
I didn't scroll across in the box!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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