I am trying to create a macro that will sub/superscript respectively when surrounded by "_" or "^". I have tried a few ways but haven't used VBA for a while and need some help. Below is the code that works if only subscript or superscript is needed but not when both need to be performed. Will I have to replace the split and join with another method when both need to be performed?
Code:
Sub Super_and_Subscript()
S_Script ("^")
S_Script ("_")
End Sub
Sub S_Script(Symbol As String)
Dim i As Integer
'
' Macro1 Macro
'
Dim temp() As String
Dim rngSTART As Range, r As Range
Dim sngSTART!, sngLEN!
On Error GoTo HANDLERR
For Each r In Selection
Set rngSTART = r
'If Right(rngSTART.Value, 1) <> Symbol Then rngSTART.Value = rngSTART.Value & Symbol
temp() = Split(r, Symbol)
rngSTART.Value2 = Join(temp(), "")
sngSTART = 1
For i = 0 To UBound(temp()) Step 2
Select Case True
Case Len(temp(i + 1)) > 0
sngSTART = Len(temp(i)) + 0 + sngSTART + sngLEN
sngLEN = Len(temp(i + 1))
'need to update to the current position & length
Select Case True
Case Symbol = "^"
rngSTART.Characters(sngSTART, sngLEN).Font.Superscript = True
Case Symbol = "_"
rngSTART.Characters(sngSTART, sngLEN).Font.Subscript = True
End Select
End Select
Next
sngSTART = 0
sngLEN = 0
Next
Exit Sub
HANDLERR:
End Sub