The inverse functions of sine and
cosine work perfectly in Excel.
However, as soon as you want to use
these in a VBA project, they stop
working.
When compared to a spreadsheet, Visual Basic for Applications (VBA) is very sparsely equipped with functions. Many additional functions however, can be constructed mathematically from the existing default functions. If you use the more complex functions more often, you should define these as VBA functions and save them in a generally available module. To enable the Visual Basic Editor click the ‘Office Button’ and choose ‘Excel options’. Select ‘Show Developer tab in Ribbon’ under the tab named ‘Popular’.
Navigate to the new ‘Developer’ tab and select ‘Visual Basic’. Generate the new module with Insert | Module. Enter the following lines of code:
Public Function PI() As Double
PI = 4 * Atn(1)
End Function
Public Function Arccos (x As Double) As Double
If x = 1 Then
Arccos = 0
ElseIf x = -1 Then
Arccos = PI()
Else Arccos = Atn(-x / Sqr((-x
* x ) + 1)) + PI() / 2
End If
End Function
Public Function Arcsin(x AS
Double) As Double
If x = 1 Then
Arcsin = PI() / 2
ElseIf x = -1 Then
Arscin = PI() / 2
Else Arcsin = Atn(x / Sqr((-x *
x) + 1))
End If
End Function
This defines the required Pi over the existing arc function ‘arctan’. The other functions will mathematically reduce ‘arcsin’ and ‘arccos’ to ‘arctan’ with the help of Pi. If you run the VBA project directly in Excel, you can go back to the functions of the worksheet. In this variant, you do not need any self-created functions; you call up the form ‘Excel.WorksheetFunction. Pi()’ instead. Set the known spreadsheet after ‘Excel.WorksheetFunction.’. The only prerequisite for this is that the reference to the respective library is activated. This is the default setting in Excel. To check, open the VBA editor and then the command ‘Tools | References’. The option ‘Microsoft Excel 11.0 Object Library’ must be activated in the list.
When compared to a spreadsheet, Visual Basic for Applications (VBA) is very sparsely equipped with functions. Many additional functions however, can be constructed mathematically from the existing default functions. If you use the more complex functions more often, you should define these as VBA functions and save them in a generally available module. To enable the Visual Basic Editor click the ‘Office Button’ and choose ‘Excel options’. Select ‘Show Developer tab in Ribbon’ under the tab named ‘Popular’.
Navigate to the new ‘Developer’ tab and select ‘Visual Basic’. Generate the new module with Insert | Module. Enter the following lines of code:
Public Function PI() As Double
PI = 4 * Atn(1)
End Function
Public Function Arccos (x As Double) As Double
If x = 1 Then
Arccos = 0
ElseIf x = -1 Then
Arccos = PI()
Else Arccos = Atn(-x / Sqr((-x
* x ) + 1)) + PI() / 2
End If
End Function
Public Function Arcsin(x AS
Double) As Double
If x = 1 Then
Arcsin = PI() / 2
ElseIf x = -1 Then
Arscin = PI() / 2
Else Arcsin = Atn(x / Sqr((-x *
x) + 1))
End If
End Function
This defines the required Pi over the existing arc function ‘arctan’. The other functions will mathematically reduce ‘arcsin’ and ‘arccos’ to ‘arctan’ with the help of Pi. If you run the VBA project directly in Excel, you can go back to the functions of the worksheet. In this variant, you do not need any self-created functions; you call up the form ‘Excel.WorksheetFunction. Pi()’ instead. Set the known spreadsheet after ‘Excel.WorksheetFunction.’. The only prerequisite for this is that the reference to the respective library is activated. This is the default setting in Excel. To check, open the VBA editor and then the command ‘Tools | References’. The option ‘Microsoft Excel 11.0 Object Library’ must be activated in the list.
0 comments:
Post a Comment
please write your comment
Note: Only a member of this blog may post a comment.