Results 1 to 12 of 12

Thread: Using SIN with VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    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. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    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. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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
    W o t . S i g

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    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. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Using SIN with VBA

    You have your radians and degrees mixed up.

    Code:
    MsgBox Sin(90/180*3.1416)
    W o t . S i g

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    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. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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 09: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

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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 10:49 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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 05:29 AM.
    W o t . S i g

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width