# Exponential in a polynomial equation - VBA

#### Oli_B

##### New Member
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).

#### c_m

##### Well-known Member
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``````

#### Oli_B

##### New Member
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

Replies
13
Views
3K
Replies
2
Views
148
Replies
3
Views
307
Replies
1
Views
340
Replies
2
Views
306

1,191,170
Messages
5,985,066
Members
439,938
Latest member
MAlhash

### 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.

### Which adblocker are you using?

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

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