VBA UDF with Cell Value Variable Not Re-Caculating

rickincanada

Board Regular
Joined
Aug 31, 2010
Messages
61
Hello,

I have a created a VBA Function that declares and then uses the value from cell A1 on Sheet1 (data validation displays a list of acceptable values) inside the function. The problem I'm having is that I'm then using that function to calculate a value in cell A1 on Sheet2. When I change the value on Sheet1 the function does not re-calculate on Sheet2. I've tried adding something along these lines but it isn't working.


Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Address = "$A$1 Then
Worksheets("Sheet2").Activate
ActiveSheet.Calculate
Worksheets("Sheet1").Activate
Range("A1").Select
End If
End Sub

Please help!!

Thanks,
Rick
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Function FinPayment(P As Double, J As Double, N As Double) As Double

Dim CalcMethod As String

CalcMethod = Sheets("Sheet1").Range("A1").Value

If CalcMethod = "Standard" Then

FinPayment = (P * (J / (1 - (1 + J) ^ -N))) / (1 + J)

ElseIf CalcMethod = "Simple" Then

FinPayment = P * (J / (1 - (1 + J) ^ -N))

End If

End Function
 
Upvote 0
Quickly tested, but this seems to work:

Add .Volatile to your UDF:
Rich (BB code):
Option Explicit
    
Function FinPayment(P As Double, J As Double, N As Double) As Double
Dim CalcMethod As String
    
    Application.Volatile
    CalcMethod = Sheets("Sheet1").Range("A1").Value
    If CalcMethod = "Standard" Then
        FinPayment = (P * (J / (1 - (1 + J) ^ -N))) / (1 + J)
    ElseIf CalcMethod = "Simple" Then
        FinPayment = P * (J / (1 - (1 + J) ^ -N))
    End If
End Function

...and force the sheet to calculate as you were...
Rich (BB code):
Option Explicit
    
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And Target.Address = "$A$1" Then
        Sheet2.Calculate
    End If
End Sub
Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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