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.
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!
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!