
Feb 26th, 2010, 05:57 PM
#1
Thread Starter
New Member
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

Feb 26th, 2010, 06:11 PM
#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.

Feb 26th, 2010, 06:26 PM
#3
Thread Starter
New Member
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

Feb 26th, 2010, 06:39 PM
#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

Feb 26th, 2010, 06:53 PM
#5
Thread Starter
New Member
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

Feb 26th, 2010, 07:04 PM
#6
Re: Using SIN with VBA
You have your radians and degrees mixed up.
Code:
MsgBox Sin(90/180*3.1416)

Feb 26th, 2010, 07:23 PM
#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

Feb 26th, 2010, 07:25 PM
#8
Thread Starter
New Member
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.

Feb 26th, 2010, 08:04 PM
#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
Last edited by westconn1; Feb 26th, 2010 at 08:08 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete

Feb 26th, 2010, 09:46 PM
#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)
Last edited by anhn; Feb 26th, 2010 at 09:49 PM.

Feb 27th, 2010, 04:15 AM
#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.
Last edited by Milk; Feb 27th, 2010 at 04:29 AM.
W o t . S i g

Feb 27th, 2010, 07:07 AM
#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

Forum Rules

Click Here to Expand Forum to Full Width
