Update user-defined functions

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
Cell A1 of Sheet2 has a value of 1.

If I type in cell A1 of Sheet1:

Code:
=MyFn()

I get a value of 100, as expected because of the code below:

Code:
Option Explicit

Function MyFn() As Double
    
    Select Case Sheet2.Cells(1, 1).Value
    
        Case 1
    
            MyFn = 100
            
        Case 2
        
            MyFn = 2000
                
        Case Else
        
            MyFn = 30000
                
    End Select
    
End Function

However, if I change the value in cell A1 of Sheet2 to 2, I expected the value in cell A1 of Sheet1 to be 2000 AUTOMATICALLY but it doesn't. Instead, I have to manually type into cell A1 of Sheet1:

Code:
= MyFn()

then it returns a value of 2000.

I assume this is the normal behaviour of a user-defined function?

My workaround is to add the following event in Sheet1:

Code:
Option Explicit

Private Sub Worksheet_Activate()

    Me.Cells(1, 1).Formula = Me.Cells(1, 1).Formula

End Sub

Is this OK or is there a better solution, such as an event that when Sheet1 is activated, all formulae in Sheet1 are updated?

Thanks
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I would try something like

Code:
Function MyFn(myVal As Long) As Double
    
    Select Case myVal
        Case 1
            MyFn = 100
        Case 2
            MyFn = 2000
        Case Else
            MyFn = 30000
    End Select
    
End Function

In Sheet1 A1
=MyFn(Sheet2!A1)

M.
 
Upvote 0
So you want the value return by your UDF to update automatically as the value in A1 changes, right?
That does not happen by default unless you add "Application.Volatile", i.e.
Code:
Function MyFn() As Double

[COLOR=#ff0000]    Application.Volatile[/COLOR]

    Select Case Sheet2.Cells(1, 1).Value
        Case 1
            MyFn = 100
        Case 2
            MyFn = 2000
        Case Else
            MyFn = 30000
    End Select
    
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

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