Exponential in a polynomial equation - VBA

Oli_B

New Member
Joined
Mar 23, 2009
Messages
2
Hi,

I am unable to get the following polynomial to work in VBA. I suspect it is because I am using ^ rather than EXP(), but I am not certain. I get the correct result when 'val = 0-20', but not when 'Val = 20.2 - 40' The code is:

Code:
Private Function AgeTransform(Val As Double) As Double


'this works fine
 If Val >= 0 And Val <= 20 Then
        AgeTransform = 0.000000000000322 + 23.954 * Val

'this is causing the problem
    ElseIf Val >= 20.1 And Val <= 40 Then
        AgeTransform = -811.473175258781 + 0.16396934404152 * Val ^ 1 + -5.61856987399637E-06 * Val ^2

    Else: Exit Function
        
    End If
    
End Function
Can anyone tell me where I am going wrong?

Thanks!

Oli
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Oli,
Welcome to the Board.

Try:
Code:
Function AgeTransform(Val As Double) As Double

If Val >= 0 And Val <= 20 Then
AgeTransform = 0.000000000000322 + 23.954 * Val
Else
If Val > 20 And Val <= 40 Then
AgeTransform = -811.473175258781 + 0.16396934404152 * Val - 5.61856987399637E-06 * (Val ^ 2)
Else
Exit Function
End If
End If
    
End Function
 
Upvote 0
Thanks for your reply c_m.
Its working fine now.
The polynomial was inverted somehow, so the problem was the prior to the code, but it's good to know that ^ is the appropriate symbol.
Thanks,
Oli
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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