Convert Digits To Subscript (H₂SO₄)
Excel Formula Improvement (Edit)
- A huge improvement was suggested by David Leal in the comments:
=LET(d,A1,
ds,MID(d,SEQUENCE(LEN(d)),1),
TEXTJOIN("",,IFERROR(UNICHAR(832&ds),ds)))
- The LAMBDA function would be something like this:
=LAMBDA(Compound,
LET(ds,MID(Compound,SEQUENCE(LEN(Compound)),1),
TEXTJOIN("",,IFERROR(UNICHAR(832&ds),ds))))
See in the continuation how to use it.
Excel Formula (Function)
=LET(d,A1,
ds,MID(d,SEQUENCE(LEN(d)),1),
s,MOD(SEQUENCE(10),10),
TEXTJOIN("",,IF(ISNUMBER(MATCH(--ds,s,0)),UNICHAR(832&ds),ds)))
- You could create a LAMBDA function:
=LAMBDA(Compound,LET(
ds,MID(Compound,SEQUENCE(LEN(Compound)),1),
s,MOD(SEQUENCE(10),10),
TEXTJOIN("",,IF(ISNUMBER(MATCH(--ds,s,0)),UNICHAR(832&ds),ds))))
-
Using Ribbon->Formulas->Defined Names->Define Name, you need to come up with a name (e.g. Chem, Compound, SubComp...) and enter it into the Name: box and enter the formula into the Refers to: box. Now you can use the function e.g. simply with
=Chem(A1)
=Chem("H2SO4")
-
Note that you can use the function in the entire workbook.
VBA
Function Chemis(ByVal Compound As String) As String
Dim n As Long, Char As String, Comp As String
For n = 1 To Len(Compound)
Char = Mid(Compound, n, 1)
If Char Like "#" Then
Comp = Comp & ChrW(832 & Char)
Else
Comp = Comp & Char
End If
Next n
Chemis = Comp
End Function