PDA

View Full Version : Basic VBA Function to calculate the volume of a cone



pedrosc
04-25-2020, 02:49 AM
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

paulked
04-25-2020, 03:17 AM
Works for me. What do you get when you bring up the formula wizard?

26440

pedrosc
04-25-2020, 03:56 AM
Works for me. What do you get when you bring up the formula wizard?

26440

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

paulked
04-25-2020, 04:01 AM
:thumb

Paul_Hossler
04-25-2020, 06:38 AM
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

Aussiebear
07-04-2022, 12:11 AM
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