How do you ArcSin in Microsoft Visual Basic?

Kevmcneilly

New Member
Joined
Jun 4, 2005
Messages
8
I need to include the "Asin()" command in a code that I am writing, but every time I do it a "Compile Error: Sub or Function not defined" message pops up. I have tried it for just the "Sin()" command and it worked fine.

I tried the help menus and searching on the web, but there seems to be nothing on this.

This doesn't work:

ArcAngle = (180 / 3.14159) * ((Asin(Length / 4) / CLRadius))

But this does:

ArcAngle = (180 / 3.14159) * ((Sin(Length / 4) / CLRadius))

Any help?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You can access many of excels worksheet functions by using the following syntx ... :-D :wink:

Code:
Public Sub demo()
    result = Application.WorksheetFunction.Asin(4 / 6)
    MsgBox result
End Sub
 
Upvote 0
G'day,

The ASin function belongs to the WorksheetFunction class in Excel's library so you need to prefix it with the text "WorksheetFunction". The Sin function is part of the VBA library which is why it works on its own.

E.g. this should work:

Code:
ArcAngle = (180 / 3.14159) * ((WorksheetFunction.Asin(Length / 4) / CLRadius))

If you're doing a lot of calculations and want to avoid having to type the text "WorksheetFunction" over and over you could do something like this:

Code:
Sub ArcSinExample()
Dim ArcAngle As Double
Dim Length As Double
Dim CLRadius As Double

Dim WF As WorksheetFunction

Length = 2
CLRadius = 5


'Set a variable WF to represent Excel's WorksheetFunction class
Set WF = Excel.WorksheetFunction


'Now you can use the variable WF in place of WorksheetFunction
ArcAngle = (180 / 3.14159) * ((WF.Asin(Length / 4) / CLRadius))


End Sub

EDIT: You may also be interested to know that the WorksheetFunction class also provides a function which will return the value of PI, rather than having to type it in all the time e.g.

Code:
'Now you can use the variable WF in place of WorksheetFunction
ArcAngle = (180 / WF.Pi) * ((WF.Asin(Length / 4) / CLRadius))

END EDIT

Hope that helps,

Dan
 
Upvote 0
If you want to shy away from worksheetfunction, which after all is a method of the Excel object, you may want to check 'Derived Math Functions' (w/o the quotes) in XL VBA help.
 
Upvote 0

Forum statistics

Threads
1,222,628
Messages
6,167,177
Members
452,101
Latest member
xuebohan

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