Consulting

Results 1 to 6 of 6

Thread: Basic VBA Function to calculate the volume of a cone

  1. #1
    VBAX Regular
    Joined
    Apr 2020
    Posts
    9
    Location

    Basic VBA Function to calculate the volume of a cone

    Hello Folks,

    I am trying to write a function that writes a code to calculate the volume of a cone. The function seems alright to me but when using it on excel I keep getting the error #NAME? . I know this must be very basic but can you please help me fixing this? The code is below...

    Function vcone(r As Double, h As Double) As Double
    Dim pi As Double
    pi = 4 * Atn(1)
    vcone = 1 / 3 * pi * r ^ 2 * h
    End Function

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Works for me. What do you get when you bring up the formula wizard?

    67227a.png
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Regular
    Joined
    Apr 2020
    Posts
    9
    Location
    Quote Originally Posted by paulked View Post
    Works for me. What do you get when you bring up the formula wizard?

    67227a.png
    It seems to be working now....I think I was using something that was defined in another module...

  4. #4
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This works also


    Option Explicit
    
    
    Function VolCone(r As Double, h As Double) As Double
        Dim Pie As Double
        
        Pie = Application.WorksheetFunction.pi
        
        VolCone = (1 / 3) * Pie * (r ^ 2) * h
    
    
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Quote Originally Posted by Luisa View Post
    This is a basic function to calculate the volume of a cone. The user enters the radius and height of a cone. The function returns the volume in the unit specified by the user.
    Incorrect the correct function is as posted in post #5
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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