Using SIN with VBA-VBForums

Thread: Using SIN with VBA

1. Using SIN with VBA

Can anyone help?
I am having quite problem with converting Radian to Degrees using VBA. I do not know how to achive this.
The code goes like this:

Private Sub Sinus()
Dim Number As Double
Number = Sin(90)
MsgBox Number
End Sub
What I need is to convert radian to degrees using VBA sin function and get correct reuslt in MsgBox. As far as I know it should return "1", but it does not. Probably because VBA Sin function uses Radian.

Thanks for replay in advance

2. Re: Using SIN with VBA

Have you tried searching the forums for: radians
I'd bet you find your answer within the first couple of hits.

3. Re: Using SIN with VBA

Believe me, Yes!
I have been searching forum more then half hour, but as I am beginer with VBA this is probably why it is hard to find

4. Re: Using SIN with VBA

your googlefu is weak my child, "degrees to radians" should get you there in one hit.

There are 360 degrees and 2*Pi radians in a circle.

Degrees / 180 * Pi = Radians

Radians / Pi * 180= Degrees

5. Re: Using SIN with VBA

Tried something like that already and it does not work correctly. It returns some strange values for 90 and 180 and it should return 1 and 0 as far as I know. I am trying several hours and it doesnt get better

This is my code and I am probably doing something wrong

Code:
```Private Sub Sinus()
Dim Radians As Double
Radians = 90
Dim Degrees As Double
Degrees = Radians * 180 / 3.14159265358979
Dim Number As Double
Number = Sin(Degrees)
MsgBox Number
End Sub```

6. Re: Using SIN with VBA

You have your radians and degrees mixed up.

Code:
`MsgBox Sin(90/180*3.1416)`

7. Re: Using SIN with VBA

there's still a problem with the formula....
Radians * 180 / 3.14159265358979

Order of precedence means it will do the multiplication first.... Rad * 180 .... and then devide THAT result by PI.... which isn't correct... putting parens around the operations will correct it...

degrees = Radians * (180 / 3.14159265358979)
Radians = Degrees * (3.14159265358979 / 180)

-tg

8. Re: Using SIN with VBA

Sorry for missunderstanding.
Because I am using this code in VBA this is for me quite hard:

Code:
```Private Sub Sinus()
Dim Number As Double
Number = Sin(90)
MsgBox Number
End Sub```
I have to calculate sinus from number 90 with my code, but this number have to be calculated as degrees, not radians. Becuase VBA sinus function calculates by default within radians - thats my problem.

This number should be presented and calculated within Degrees, not with radians. What I have to do, or what I have to change to calculate sinus within degreees, not with radians with VBA.

So the problem as far as I can see is about changing some parameters inside sinus functiona and I do supose there is no easy way to do this with this block of the code.

I have to convert sinus function to calculate sinus by degrees, not by radians.

Anyway, thanks for a help.

9. Re: Using SIN with VBA

Order of precedence means it will do the multiplication first.
while i agree order of precedence is sometimes critical i don't see it as being so in this case
?90 *3.14159265358979 / 180
1.5707963267949
? 90 *(3.14159265358979 / 180)
1.5707963267949
Code:
```Const radians As Double = 3.14159265358979 / 180
MsgBox Sin(90 * radians)```
if you declare a constant in the general section you can use it throughout the form

10. Re: Using SIN with VBA

Functions on degrees:
Code:
```'-- pi = 4 * Atn(1)
'-- Atn(1)= (pi/4) radian = 45 degrees
'-- 1 degree = pi/180 radian = (pi/4)/45 radian = Atn(1)/45 radian

Function SinD(d As Double) As Double
SinD = Sin(d * Atn(1) / 45)
End Function

Function CosD(d As Double) As Double
CosD = Cos(d * Atn(1) / 45)
End Function```
Code:
```    Dim d As Double

d = 60
Debug.Print "Cos(" & d & ")="; CosD(d)
Debug.Print "Sin(" & d & ")="; SinD(d)```

11. Re: Using SIN with VBA

vb c#
The only thing I don't like about using Atn(1) * 4 for Pi is that it can't be used to define a constant and Atn() is relatively slow. Unfortunately you can't get quite the same precision using a double literal because it gets truncated, you can however coerce a string literal without truncation.
Code:
```    'Const PI As Double = Atn(1) * 4# '<~~ ERROR can't derive a constant from a function
Const PIE As Double = 3.14159265358979
Const PI As Double = "3.141592653589793"

Dim Atn1x4 As Double
Atn1x4 = Atn(1) * 4#

Debug.Print Atn1x4 = PI 'returns true
Debug.Print Atn1x4 = PIE 'returns false```
Using a constant has the advantage that parts of a calculation can be evaluated at compile time
Code:
`Circumference = Radius * (PI * 2#) '<~~ PI * 2# calculated at compile time`
I'm perhaps being a little anal here

Edit: @Nedim, I'm not sure if you are still struggling but Sin() in both VB and VBA can never accept degrees. You can never have Sin(degrees) but hopefully you can see how easy it is to convert degrees to radians.

12. Re: Using SIN with VBA

Moved To Office Development

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width