VBA , declare variables for all funtions within module

Daniel89

New Member
Joined
Mar 14, 2018
Messages
26
Hi,

I am a Novice in programming, but I have made a calculation tool that solves certain engineering problems. In the main module there is about 10 different functions that uses many of the same variables. To make an easy solution for all functions and not risking typos in new functions and so on I would like to declare common variables at the top of the module, this variables will be used for many functions, and will also make the tool more flexible as it is possible to put in other variables to be used for other fluids for example, instead of changing all the functions.

Code:
Option Explicit

Sub Declare_variables()
Dim Pi, g, Tc, Pc, R, M, Mw, accFact
Pi = 3.14159265358979
g = 9.81 'm/s2
Tc = 126.15 'K, critical temperature for nitrogen
Pc = 3397800 'Pa, critical pressure for nitrogen
R = 8.3145 'J/mol*K ' universal gas constant
M = 28.01348 'kg/kmol , molar weight of nitrogen
Mw = 0.02801348 'kg/mol, molar weight of nitrogen kg/mol
accFact = 0.04 ' Accentric Factor for N2
End Sub

Function Kelvin(C)
'Adds 273.15 to the input Celsius value to convert to Kelvin temperature
Kelvin = C + 273.15
End Function
Function Pressure_Pa(P_Bar)
'Function to convert Bar to Pascal pressure
Pressure_Pa = P_Bar * 10 ^ 5
End Function

Function SRK_N2_Z(Bar, Celsius)
 'Date: 08.02.2018
 'Purpose: Calculate Z factor of N2 by Soavey Redlich Kwong Equation of state with iterations until error<10^-6
 'Input:
 'Pressure in Bar
 'Temperature in Celsius
 TempK = Kelvin(Celsius) 'Kelvin
 PressurePa = Pressure_Pa(Bar) 'Pa
 afactorN2 = 0.42748 * ((R ^ 2 * Tc ^ 2) / Pc)
 bfactorN2 = 0.08664 * ((R * Tc) / (Pc))
 Tr = TempK / Tc
 Pr = PressurePa / Pc
 alpha = (1 + (0.48508 + (1.55171 * accFact) - (0.15613 * accFact ^ 2)) * (1 - ((Tr) ^ (1 / 2)))) ^ 2
 srkAfactor = (afactorN2 * alpha * PressurePa) / (R ^ 2 * TempK ^ 2)
 srkBfactor = (bfactorN2 * PressurePa) / (R * TempK)
 NewtonRhapsonZ = PressurePa / (R * TempK)
 NewtonRhapsonA = (0.42748 * alpha * Pr) / (Tr ^ 2)
 NewtonRhapsonB = (0.08664 * Pr) / Tr
 Znew = 1 'initial guess
 For i = 1 To 100
 Z = Znew
 F_Z = (Z ^ 3) - (Z ^ 2) + ((NewtonRhapsonA - NewtonRhapsonB - NewtonRhapsonB ^ 2) * Z) - (NewtonRhapsonA * NewtonRhapsonB)
 D_Z = 3 * Z ^ 2 - 2 * Z + (NewtonRhapsonA - NewtonRhapsonB - NewtonRhapsonB ^ 2)
 Znew = Z - (F_Z / D_Z)
 SRK_N2_Z = Znew
 If Abs(Znew - Z) < 10 ^ -6 Then Exit For
    Next i
If i > 100 Then End
End Function

It isn't working, the SRK_N2_Z function is not taking the variables from the sub and I get the error:

Variable not defined!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Declare those common parameters at the top of the module like in the example below, not in a subroutine.
Code:
Option Explicit
Const Pi As Double = 3.14159
Function Circle_Area(D As Double) As Double
Circle_Area = Pi * D
End Function
Sub test()
Dim Dia As Double
Dia = 1
MsgBox "The area of a circle having diameter " & Dia & " is " & Circle_Area(Dia)
End Sub
 
Upvote 0
Post the code that "doesn't work" and tell us exactly what happens when you run it. If you get an error message, what is the message and what line is causing it?
 
Upvote 0
Hey,

it said variable not defined. But I found the solution, this is working fine for me now:
Code:
Option Explicit
' To accomodate the calculation tool for other fluid than Nitrogen, change Tc, Pc, M, Mw and accFact to fit fluid properties. Note! The Lemmon - Jacobsen viscosity equation will not give precise results for other gas than Nitrogen!
Public Const Pi As Double = 3.14159265358979
Public Const g As Double = 9.81 'm/s2, accelration of gravity
Public Const Tc As Double = 126.192 'K, Critical temperature for Nitrogen
Public Const M As Double = 28.01348 'kg/kmol, Molar mass for Nitrogen kg/kmol, g/mol
Public Const Rhocrit As Double = (11.1839 * M) ' g/dm3, kg/m3 critical density for Nitrogen
Public Const Pc As Double = 3397800 'Pa, critical pressure for Nitrogen
Public Const R As Double = 8.3145 'J/mol*K , ideal gas constant
Public Const Mw As Double = 0.02801348 'kg/mol, Molar mass for Nitrogen kg/mol (M/1000)
Public Const accFact As Double = 0.04 ' Accentric Factor for Nitrogen
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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