UDF issue... AGAIN!

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Hi guys,

Can anyone see any possible issues with the following UDF?

Code:
Public Function Vel(Pressure As Double, Flow As Double, Diameter As Double) As Double
Dim z As Double
Dim Area As Double

Area = (Pi * (Diameter / 2) ^ 2) / 1000
z = 1 - 0.00248 * (Pressure + 1.01325)
Vel = Flow * 1000000 / 86400 / Area * (1.01325 / (Pressure + 1.01325)) * z / 0.99978
End Function

It should be used to calculate the flow rate of gas in a pipeline. However, it is just returning #VALUE!

Any ideas?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Pi is not a pre-defined variable; it is a worksheet function. Try:

Code:
Public Function Vel(Pressure As Double, Flow As Double, Diameter As Double) As Double
Dim z As Double
Dim Area As Double
 
Area = (WorksheetFunction.Pi * (Diameter / 2) ^ 2) / 1000
z = 1 - 0.00248 * (Pressure + 1.01325)
Vel = Flow * 1000000 / 86400 / Area * (1.01325 / (Pressure + 1.01325)) * z / 0.99978
End Function
 
Upvote 0
Don't you have to declare/set "Pi"?
I tested it out in VBA, and Excel doesn't seem to identify the variable "Pi" with the actual value of "Pi".
 
Upvote 0
Where would I be without you MrKowz? Obviously you are right... again ;)

Thanks very much mate
 
Upvote 0
Where would I be without you MrKowz? Obviously you are right... again ;)

Thanks very much mate

Happy to be of (continued) assistance! Thank you for the feedback. :biggrin:
 
Upvote 0
Don't you have to declare/set "Pi"?
I tested it out in VBA, and Excel doesn't seem to identify the variable "Pi" with the actual value of "Pi".

If you wanted to use the variable Pi in multiple areas, and you don't want to call it with WorksheetFunction.Pi each time, you can declare it as a Public Const:

Code:
Option Explicit
Public Const Pi As Double = 3.14159265358979
 
Public Function Vel(Pressure As Double, Flow As Double, Diameter As Double) As Double
Dim z As Double
Dim Area As Double
Area = (Pi * (Diameter / 2) ^ 2) / 1000
z = 1 - 0.00248 * (Pressure + 1.01325)
Vel = Flow * 1000000 / 86400 / Area * (1.01325 / (Pressure + 1.01325)) * z / 0.99978
End Function

Note, even though you are defining Pi to a fixed number of decimal places, Excel's Floating Point Decimal approximation restricts you to that limited number of decimal places anyway.
 
Upvote 0
Actually, I didn't add much that MrKowz didn't already say, I am just a much slower typist, so I didn't see his reply!
 
Upvote 0
If you wanted to use the variable Pi in multiple areas, and you don't want to call it with WorksheetFunction.Pi each time, you can declare it as a Public Const:

... or not public, inside the function


Code:
Option Explicit
 
Public Function Vel(Pressure As Double, Flow As Double, Diameter As Double) As Double
Const Pi As Double = 3.14159265358979
...
 
Upvote 0
... or not public, inside the function


Code:
Option Explicit
 
Public Function Vel(Pressure As Double, Flow As Double, Diameter As Double) As Double
Const Pi As Double = 3.14159265358979
...
If there is only one function that uses Pi, this would work nicely. However, if there are multiple UDFs that need "Pi" in the workbook, declaring a single public constant would likely be ideal. ;)
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,480
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top