Forum Discussion
Addition of alphanumeric values, such as combine two or more molecular formula in one.
- Aug 11, 2021
You might look around to see if there is software available (maybe even an excel add-in) that can do this.
In the meantime, you could try this user defined function. To view the function you would need to go into the vba editor (Alt+F11), find the workbook in the project explorer (Ctrl+R if project explorer window is not open), expand the workbook and double click on Module1. Then, you could drag/drop the module into other workbooks. To use, you would enter =chemformula(A1,B1) where your two formulas are in A1 and B1. I attached a workbook as well.
Public Function ChemFormula(formula1 As String, formula2 As String) As Variant Const elemPattern As String = "([A-Z][a-z]?)(\d+)?" Static regEx As Object Dim matches As Object Dim elements As Object Dim molecules As Long Dim outText As String Dim i As Long Dim dicKey As Variant On Error GoTo ErrHandler If regEx Is Nothing Then Set regEx = CreateObject("vbscript.regexp") With regEx .Global = True: .IgnoreCase = False: .MultiLine = False: .Pattern = elemPattern End With End If With regEx If .Test(formula1 & formula2) Then Set matches = .Execute(formula1 & formula2) Set elements = CreateObject("Scripting.Dictionary") For i = 0 To matches.Count - 1 With matches(i) If Len(.SubMatches(1)) = 0 Then molecules = 1 Else molecules = CLng(.SubMatches(1)) End If If elements.Exists(.SubMatches(0)) Then elements(.SubMatches(0)) = elements(.SubMatches(0)) + molecules Else elements(.SubMatches(0)) = molecules End If End With Next i For Each dicKey In elements.Keys outText = outText & dicKey If elements(dicKey) > 1 Then outText = outText & elements(dicKey) End If Next dicKey Else Err.Raise Number:=vbObjectError + 512 End If End With ChemFormula = outText ExitProc: Exit Function ErrHandler: ChemFormula = CVErr(xlErrValue) Resume ExitProc End Function
You might look around to see if there is software available (maybe even an excel add-in) that can do this.
In the meantime, you could try this user defined function. To view the function you would need to go into the vba editor (Alt+F11), find the workbook in the project explorer (Ctrl+R if project explorer window is not open), expand the workbook and double click on Module1. Then, you could drag/drop the module into other workbooks. To use, you would enter =chemformula(A1,B1) where your two formulas are in A1 and B1. I attached a workbook as well.
Public Function ChemFormula(formula1 As String, formula2 As String) As Variant
Const elemPattern As String = "([A-Z][a-z]?)(\d+)?"
Static regEx As Object
Dim matches As Object
Dim elements As Object
Dim molecules As Long
Dim outText As String
Dim i As Long
Dim dicKey As Variant
On Error GoTo ErrHandler
If regEx Is Nothing Then
Set regEx = CreateObject("vbscript.regexp")
With regEx
.Global = True: .IgnoreCase = False: .MultiLine = False: .Pattern = elemPattern
End With
End If
With regEx
If .Test(formula1 & formula2) Then
Set matches = .Execute(formula1 & formula2)
Set elements = CreateObject("Scripting.Dictionary")
For i = 0 To matches.Count - 1
With matches(i)
If Len(.SubMatches(1)) = 0 Then
molecules = 1
Else
molecules = CLng(.SubMatches(1))
End If
If elements.Exists(.SubMatches(0)) Then
elements(.SubMatches(0)) = elements(.SubMatches(0)) + molecules
Else
elements(.SubMatches(0)) = molecules
End If
End With
Next i
For Each dicKey In elements.Keys
outText = outText & dicKey
If elements(dicKey) > 1 Then
outText = outText & elements(dicKey)
End If
Next dicKey
Else
Err.Raise Number:=vbObjectError + 512
End If
End With
ChemFormula = outText
ExitProc:
Exit Function
ErrHandler:
ChemFormula = CVErr(xlErrValue)
Resume ExitProc
End Function