Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a VBA subroutine to compute the average molecular weight of a gas mixture

ID: 3815060 • Letter: W

Question

Write a VBA subroutine to compute the average molecular weight of a gas mixture (MW_average) with the following composition by mass (mass of component/total mass). Recall: MW_average = sigma_i y_i M_i/i where is the component mole fraction and M_i is the component molecular weight. The user should specify the gas composition in mass fraction (i.e. g of component A/g of total gas) and your program should do all necessary calculations. Demonstrate your subroutine functions correctly by computing the average molecular weight of a gas composed of; O_2 16% CO 4% CO_2 17% N_2 63%

Explanation / Answer

Public Function Molecular_Weight(sCMPND As String) As Double
Dim sTMP As String, i As Long, sEL As String, sSB As String
Dim dAW As Double, dAWEIGHT As Double, dSUB As Long
sTMP = sCMPND: dAWEIGHT = 0: sSB = "0": sEL = vbNullString
Do While CBool(Len(sTMP))
sSB = "0": sEL = vbNullString
If Asc(Mid(sTMP, Application.Min(2, Len(sTMP)), 1)) > 96 Then
sEL = Left(sTMP, 2)
Else
sEL = Left(sTMP, 1)
End If
sTMP = Right(sTMP, Len(sTMP) - Len(sEL))
Do While IsNumeric(Left(sTMP, 1))
sSB = sSB & Int(Left(sTMP, 1))
sTMP = Right(sTMP, Len(sTMP) - 1)
Loop
dAWEIGHT = dAWEIGHT + Application.VLookup(sEL, ThisWorkbook.Names("tblPeriodic").RefersToRange, 6, False) * (Int(sSB) - (Not CBool(Int(sSB))))
Loop
Molecular_Weight = dAWEIGHT
End Function

Public Function Formula_Alt(sCMPND As String) As String
Dim sb As Long, sCOMPOUND As String
sCOMPOUND = sCMPND
For sb = 0 To 9
sCOMPOUND = Replace(sCOMPOUND, sb, ChrW(8320 + sb))
Next sb
Formula_Alt = sCOMPOUND
End Function

Public Function Formula_Alt2(sCMPND As String) As String
Dim sb As Long, sCOMPOUND As String
sCOMPOUND = sCMPND
For sb = 0 To 9
sCOMPOUND = Replace(sCOMPOUND, ChrW(8320 + sb), sb)
Next sb
Formula_Alt2 = sCOMPOUND
End Function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote