Using a VBA function in a cell

Jeffrey Smith

Well-known Member
Joined
Feb 11, 2005
Messages
795
Is it possible to use a VBA function in a cell like this:

=QPTan2x(123,301.76) < entered into a cell

In VBA:

Function QPTan2X(X!, Y!)

Dim Tmp!, PI As Double
PI = Atn(1) * 4

If X! = 0! Then
Tmp! = Sgn(Y!) * (PI / 2!)
ElseIf X! > 0! Then
Tmp! = Atn(Y! / X!)
ElseIf Y! >= 0! Then
Tmp! = PI + Atn(Y! / X!)
Else
Tmp! = -PI + Atn(Y! / X!)
End If
Tmp! = 180 * Tmp! / PI

If Tmp! < 0! Then Tmp! = Tmp! + 360!

QPATAN2x = Tmp!

End Function
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Sure, you just need to fix the typo in the last line of your code:

QPATAN2x = Tmp!
should be
QPTan2x = Tmp!

It gave me a result of 67.82376
 
Upvote 0
It isn't a matter of Case Sensitivity, you have an extra "A" in there.

Developer Tip:
In the VB Editor under Tools | Options | Editor, click the "Require Variable Declaration" box.

Then every time you start a new macro, the words Option Explicit will appear at the top, and whenever you try to run a macro, if it finds any undeclared variables, it will tell you. That would have caught this typo.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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