Hi can someone please help with my VBA function:
The WB Function is return a #VALUE! error and I can't understand way. Here is the code:
Thank you Tom
'====================================================================================================================='
' Constants
'====================================================================================================================='
Const CONST_01 = -5674.5359
Const CONST_02 = 6.3925247
Const CONST_03 = -0.009677843
Const CONST_04 = 0.000000622157
Const CONST_05 = 2.0747825E-09
Const CONST_06 = -9.484024E-13
Const CONST_07 = 4.1635019
Const CONST_08 = -5800.2206
Const CONST_09 = 1.3914993
Const CONST_10 = -0.048640239
Const CONST_11 = 0.000041764768
Const CONST_12 = -0.000000014452093
Const CONST_13 = 6.5459673
Const CONST_14 = 6.54
Const CONST_15 = 14.526
Const CONST_16 = 0.7389
Const CONST_17 = 0.09486
Const CONST_18 = 0.4569
Const R_WATER = 18.015268
Const R_AIR = 28.964546
'1) Equation 1: Get humidity from paritial vapour pressure and saturated vapour pressure.
Function get_humidity(ByVal paritial_vapour_pressure As Variant, ByVal saturated_vapour_pressure As Variant)
get_humidity = (paritial_vapour_pressure / saturated_vapour_pressure) * 100
End Function
'5) Equation 5: Get saturated vapour pressure from dry bulb.
Function get_saturated_vapour_pressure(ByVal dry_bulb As Variant)
If dry_bulb >= 0 Then
get_saturated_vapour_pressure = (Exp(CONST_08 / (dry_bulb + 273.15) + CONST_09 + CONST_10 * (dry_bulb + 273.15) + CONST_11 * (dry_bulb + 273.15) ^ 2 + CONST_12 * (dry_bulb + 273.15) ^ 3 + CONST_13 * Log((dry_bulb + 273.15)))) / 1000
Else:
get_saturated_vapour_pressure = (Exp(CONST_01 / (dry_bulb + 273.15) + CONST_02 + CONST_03 * (dry_bulb + 273.15) + CONST_04 * (dry_bulb + 273.15) ^ 2 + CONST_05 * (dry_bulb + 273.15) ^ 3 + CONST_06 * (dry_bulb + 273.15) ^ 4 + CONST_07 * Log((dry_bulb + 273.15)))) / 1000
End If
End Function
'6) Equation 6: Get humidity ratio from dry bulb vapour pressure and pressure.
Function get_humidity_ratio(ByVal dry_bulb_vapour_pressure As Variant, ByVal pressure As Variant)
get_humidity_ratio = R_WATER / R_AIR * dry_bulb_vapour_pressure / (pressure - dry_bulb_vapour_pressure)
End Function
'7) Equation 7: Get moisture content from wet buld, humidity ratio and dry bulb.
Function get_moisture_content(ByVal wet_bulb As Variant, ByVal humidity_ratio As Variant, ByVal dry_bulb As Variant)
If dry_bulb <= 0 Then
get_moisture_content = ((2830 - 0.24 * wet_bulb) * humidity_ratio - 1.006 * (dry_bulb - wet_bulb)) / (2830 + 1.86 * dry_bulb - 2.1 * wet_bulb)
Else:
get_moisture_content = ((2501 - 2.326 * wet_bulb) * humidity_ratio - 1.006 * (dry_bulb - wet_bulb)) / (2501 + 1.86 * dry_bulb - 4.186 * wet_bulb)
End If
End Function
'8) Equation 8: Get dry bulb vapour pressure from wet bulb.
Function get_dry_bulb_vapour_pressure(ByVal wet_bulb As Variant)
If (wet_bulb + 273.15) >= 0 Then
get_dry_bulb_vapour_pressure = Exp(CONST_08 / (wet_bulb + 273.15) + CONST_09 + CONST_10 * (wet_bulb + 273.15) + CONST_11 * (wet_bulb + 273.15) ^ 2 + CONST_12 * (wet_bulb + 273.15) ^ 3 + CONST_13 * Log((wet_bulb + 273.15))) / 1000
Else:
get_dry_bulb_vapour_pressure = Exp(CONST_01 / (wet_bulb + 273.15) + CONST_02 + CONST_03 * (wet_bulb + 273.15) + CONST_04 * (wet_bulb + 273.15) ^ 2 + CONST_05 * (wet_bulb + 273.15) ^ 3 + CONST_06 * (wet_bulb + 273.15) ^ 4 + CONST_07 * Log((wet_bulb + 273.15))) / 1000
End If
End Function
'9) Equation 9: Get partial vapour pressure from pressure and moisture content.
Function get_partial_vapour_pressure(ByVal pressure As Variant, ByVal moisture_content As Variant)
get_partial_vapour_pressure = (pressure * moisture_content) / (0.621945 + moisture_content)
End Function
'10) Equation 10: Get enthalpy from dry bulb and moisture content.
Function get_enthalpy(ByVal dry_bulb As Variant, ByVal moisture_content As Variant)
get_enthalpy = 1.006 * dry_bulb + moisture_content * (2501 + 1.86 * dry_bulb)
End Function
'11) Equation 11: Get pressure from altitude.
Function get_pressure(ByVal altitude As Variant):
get_pressure = 101.325 * (1 - 0.0000225577 * altitude) ^ 5.2559
End Function
'12) Equation 12: Get specific volume from dry bulb, moisture content and pressure.
Function get_specific_volume(ByVal dry_bulb As Variant, ByVal moisture_content As Variant, ByVal pressure As Variant):
get_specific_volume = (0.287042 * (dry_bulb + 273.15)) * (1 + 1.607858 * moisture_content) / pressure
End Function
'13) Equation 13: Get dew point temperature from partial vapour pressure.
Function get_dew_point_temperature(ByVal partial_vapour_pressure As Variant)
If dry_bulb <= 93 And dry_bulb >= 0 Then
get_dew_point_temperature = 6.54 + 14.526 * Log(partial_vapour_pressure) + 0.7389 * Log(partial_vapour_pressure) * Log(partial_vapour_pressure) + 0.09486 * Log(partial_vapour_pressure) ^ 3 + 0.4569 * partial_vapour_pressure ^ 0.1984
ElseIf dry_bulb < 0 Then
get_dew_point_temperature = 6.09 + 12.608 * Log(partial_vapour_pressure) + 0.4959 * Log(partial_vapour_pressure) * Log(partial_vapour_pressure)
Else
get_dew_point_temperature = "Error"
End If
End Function
Function hum_acc(ByVal humidity As Variant, ByVal humidity_loop As Variant)
If (humidity / humidity_loop) < 1.1 And (humidity / humidity_loop) > 0.9 Then
hum_acc = True
Else
hum_acc = False
End If
End Function
Function WB(ByVal altitude As Variant, ByVal dry_bulb As Variant, ByVal humidity As Variant) As Variant
Dim wet_bulb As Variant
Dim wb_loop As Variant
Dim saturated_vapour_pressure As Variant
Dim pressure As Variant
Dim dry_bulb_vapour_pressure As Variant
Dim humidity_ratio As Variant
Dim moisture_content As Variant
Dim partial_vapour_pressure As Variant
Dim hum_ok As Boolean
wb_loop = 0.001
wet_bulb = dry_bulb
Do
wet_bulb = wet_bulb - wb_loop
saturated_vapour_pressure = get_saturated_vapour_pressure(dry_bulb)
pressure = get_pressure(altitude)
dry_bulb_vapour_pressure = get_dry_bulb_vapour_pressure(wet_bulb)
humidity_ratio = get_humidity_ratio(dry_bulb_vapour_pressure, pressure)
moisture_content = get_moisture_content(wet_bulb, humidity_ratio, dry_bulb)
partial_vapour_pressure = get_partial_vapour_pressure(pressure, moisture_content)
humidity_loop = get_humidity(paritial_vapour_pressure, saturated_vapour_pressure)
hum_ok = hum_acc(humidity, humidity_loop)
Loop Until hum_ok = True
WB = 1000
End Function
The WB Function is return a #VALUE! error and I can't understand way. Here is the code:
Thank you Tom
'====================================================================================================================='
' Constants
'====================================================================================================================='
Const CONST_01 = -5674.5359
Const CONST_02 = 6.3925247
Const CONST_03 = -0.009677843
Const CONST_04 = 0.000000622157
Const CONST_05 = 2.0747825E-09
Const CONST_06 = -9.484024E-13
Const CONST_07 = 4.1635019
Const CONST_08 = -5800.2206
Const CONST_09 = 1.3914993
Const CONST_10 = -0.048640239
Const CONST_11 = 0.000041764768
Const CONST_12 = -0.000000014452093
Const CONST_13 = 6.5459673
Const CONST_14 = 6.54
Const CONST_15 = 14.526
Const CONST_16 = 0.7389
Const CONST_17 = 0.09486
Const CONST_18 = 0.4569
Const R_WATER = 18.015268
Const R_AIR = 28.964546
'1) Equation 1: Get humidity from paritial vapour pressure and saturated vapour pressure.
Function get_humidity(ByVal paritial_vapour_pressure As Variant, ByVal saturated_vapour_pressure As Variant)
get_humidity = (paritial_vapour_pressure / saturated_vapour_pressure) * 100
End Function
'5) Equation 5: Get saturated vapour pressure from dry bulb.
Function get_saturated_vapour_pressure(ByVal dry_bulb As Variant)
If dry_bulb >= 0 Then
get_saturated_vapour_pressure = (Exp(CONST_08 / (dry_bulb + 273.15) + CONST_09 + CONST_10 * (dry_bulb + 273.15) + CONST_11 * (dry_bulb + 273.15) ^ 2 + CONST_12 * (dry_bulb + 273.15) ^ 3 + CONST_13 * Log((dry_bulb + 273.15)))) / 1000
Else:
get_saturated_vapour_pressure = (Exp(CONST_01 / (dry_bulb + 273.15) + CONST_02 + CONST_03 * (dry_bulb + 273.15) + CONST_04 * (dry_bulb + 273.15) ^ 2 + CONST_05 * (dry_bulb + 273.15) ^ 3 + CONST_06 * (dry_bulb + 273.15) ^ 4 + CONST_07 * Log((dry_bulb + 273.15)))) / 1000
End If
End Function
'6) Equation 6: Get humidity ratio from dry bulb vapour pressure and pressure.
Function get_humidity_ratio(ByVal dry_bulb_vapour_pressure As Variant, ByVal pressure As Variant)
get_humidity_ratio = R_WATER / R_AIR * dry_bulb_vapour_pressure / (pressure - dry_bulb_vapour_pressure)
End Function
'7) Equation 7: Get moisture content from wet buld, humidity ratio and dry bulb.
Function get_moisture_content(ByVal wet_bulb As Variant, ByVal humidity_ratio As Variant, ByVal dry_bulb As Variant)
If dry_bulb <= 0 Then
get_moisture_content = ((2830 - 0.24 * wet_bulb) * humidity_ratio - 1.006 * (dry_bulb - wet_bulb)) / (2830 + 1.86 * dry_bulb - 2.1 * wet_bulb)
Else:
get_moisture_content = ((2501 - 2.326 * wet_bulb) * humidity_ratio - 1.006 * (dry_bulb - wet_bulb)) / (2501 + 1.86 * dry_bulb - 4.186 * wet_bulb)
End If
End Function
'8) Equation 8: Get dry bulb vapour pressure from wet bulb.
Function get_dry_bulb_vapour_pressure(ByVal wet_bulb As Variant)
If (wet_bulb + 273.15) >= 0 Then
get_dry_bulb_vapour_pressure = Exp(CONST_08 / (wet_bulb + 273.15) + CONST_09 + CONST_10 * (wet_bulb + 273.15) + CONST_11 * (wet_bulb + 273.15) ^ 2 + CONST_12 * (wet_bulb + 273.15) ^ 3 + CONST_13 * Log((wet_bulb + 273.15))) / 1000
Else:
get_dry_bulb_vapour_pressure = Exp(CONST_01 / (wet_bulb + 273.15) + CONST_02 + CONST_03 * (wet_bulb + 273.15) + CONST_04 * (wet_bulb + 273.15) ^ 2 + CONST_05 * (wet_bulb + 273.15) ^ 3 + CONST_06 * (wet_bulb + 273.15) ^ 4 + CONST_07 * Log((wet_bulb + 273.15))) / 1000
End If
End Function
'9) Equation 9: Get partial vapour pressure from pressure and moisture content.
Function get_partial_vapour_pressure(ByVal pressure As Variant, ByVal moisture_content As Variant)
get_partial_vapour_pressure = (pressure * moisture_content) / (0.621945 + moisture_content)
End Function
'10) Equation 10: Get enthalpy from dry bulb and moisture content.
Function get_enthalpy(ByVal dry_bulb As Variant, ByVal moisture_content As Variant)
get_enthalpy = 1.006 * dry_bulb + moisture_content * (2501 + 1.86 * dry_bulb)
End Function
'11) Equation 11: Get pressure from altitude.
Function get_pressure(ByVal altitude As Variant):
get_pressure = 101.325 * (1 - 0.0000225577 * altitude) ^ 5.2559
End Function
'12) Equation 12: Get specific volume from dry bulb, moisture content and pressure.
Function get_specific_volume(ByVal dry_bulb As Variant, ByVal moisture_content As Variant, ByVal pressure As Variant):
get_specific_volume = (0.287042 * (dry_bulb + 273.15)) * (1 + 1.607858 * moisture_content) / pressure
End Function
'13) Equation 13: Get dew point temperature from partial vapour pressure.
Function get_dew_point_temperature(ByVal partial_vapour_pressure As Variant)
If dry_bulb <= 93 And dry_bulb >= 0 Then
get_dew_point_temperature = 6.54 + 14.526 * Log(partial_vapour_pressure) + 0.7389 * Log(partial_vapour_pressure) * Log(partial_vapour_pressure) + 0.09486 * Log(partial_vapour_pressure) ^ 3 + 0.4569 * partial_vapour_pressure ^ 0.1984
ElseIf dry_bulb < 0 Then
get_dew_point_temperature = 6.09 + 12.608 * Log(partial_vapour_pressure) + 0.4959 * Log(partial_vapour_pressure) * Log(partial_vapour_pressure)
Else
get_dew_point_temperature = "Error"
End If
End Function
Function hum_acc(ByVal humidity As Variant, ByVal humidity_loop As Variant)
If (humidity / humidity_loop) < 1.1 And (humidity / humidity_loop) > 0.9 Then
hum_acc = True
Else
hum_acc = False
End If
End Function
Function WB(ByVal altitude As Variant, ByVal dry_bulb As Variant, ByVal humidity As Variant) As Variant
Dim wet_bulb As Variant
Dim wb_loop As Variant
Dim saturated_vapour_pressure As Variant
Dim pressure As Variant
Dim dry_bulb_vapour_pressure As Variant
Dim humidity_ratio As Variant
Dim moisture_content As Variant
Dim partial_vapour_pressure As Variant
Dim hum_ok As Boolean
wb_loop = 0.001
wet_bulb = dry_bulb
Do
wet_bulb = wet_bulb - wb_loop
saturated_vapour_pressure = get_saturated_vapour_pressure(dry_bulb)
pressure = get_pressure(altitude)
dry_bulb_vapour_pressure = get_dry_bulb_vapour_pressure(wet_bulb)
humidity_ratio = get_humidity_ratio(dry_bulb_vapour_pressure, pressure)
moisture_content = get_moisture_content(wet_bulb, humidity_ratio, dry_bulb)
partial_vapour_pressure = get_partial_vapour_pressure(pressure, moisture_content)
humidity_loop = get_humidity(paritial_vapour_pressure, saturated_vapour_pressure)
hum_ok = hum_acc(humidity, humidity_loop)
Loop Until hum_ok = True
WB = 1000
End Function