Superscript all numeric characters after ^ in an excel cell I have material property entries in a cell those look something like this:

cell.value="O2 Permeability [cm^3/m^2]"


The goal is to format the numeric value after each ^ to be superscripted and to remove all ^.

Here my code:

Public Function FormatSuperscript(cell As Range) As Boolean
Dim i As Integer
Dim value As String
Dim cnum As New Collection
Dim num As Integer
Dim clen As New Collection

value = cell.value

num = 0

' first count the amount of ^

num = InStr(num + 1, value, "^")
If num &lt;&gt; 0 Then
    cnum.Add num
    ' count the length of the numeric entry
    clen.Add SuperScriptLength(value, num)
End If


While num &lt;&gt; 0
    num = InStr(num + 1, value, "^")
    If num &lt;&gt; 0 Then
        cnum.Add num
        ' count the length of the numeric entry
        clen.Add SuperScriptLength(value, num)
    End If
Wend

' remove all ^
cell.value = Replace(value, "^", vbNullString)

' go through all ^ places and superscript the length of the numeric entry from there on
For i = 1 To cnum.count
    cell.Characters(Start:=cnum(i) - i + 1, Length:=clen(i)).Font.Superscript = True
Next i

FormatSuperscript = True

End Function
Private Function SuperScriptLength(value As String, StartCharacter As Integer) As Integer
Dim s As String
Dim c As Integer
Dim i As Integer

c = 0
s = Right(value, Len(value) - StartCharacter)
While StringPop(s) = "-" Or IsNumeric(StringPop(s))
    c = c + 1
    s = Right(s, Len(s) - 1)
Wend

SuperScriptLength = c
End Function

Private Function StringPop(ByRef s As String) As String
StringPop = Left(s, 1)
End Function


Any suggestions on how to improve the run time on it?
Formatting individual characters in a string is relatively slow.  My solution convert the exponents to superscript using Ascii and Unicode characters.  This allows me to work with the values in an array and then write it back over the original range.  

Using characters code took 0.79 seconds to process 10,000 cells compared to 42.05 using formatting individual characters.  The file size increased by 14KB using the character codes and 50KB using character formatting.

Before &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; After

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


Code to Convert Exponents to SuperScript Characters

Function convertExponentsToSuperScript(Text As String, Optional ExponentChar As String = "^") As String
    If InStr(Text, ExponentChar) = 0 Then
        convertExponentsToSuperScript = Text
        Exit Function
    End If
    Dim newText() As String
    ReDim newText(Len(Text) - UBound(Split(Text, ExponentChar)))

    Dim exponentChars() As Variant
    exponentChars = Array(ChrW(&amp;H2070), Chr(185), Chr(178), Chr(179), _
                          ChrW(&amp;H2074), ChrW(&amp;H2075), ChrW(&amp;H2076), ChrW(&amp;H2077), _
                          ChrW(&amp;H2078), ChrW(&amp;H2079))

    Dim textIndex As Long, newTextIndex As Long
    Dim ch As String
    Dim flag As Boolean

    For textIndex = 1 To Len(Text)
        ch = Mid(Text, textIndex, 1)
        If ch = ExponentChar Then
            flag = True
        Else
            newTextIndex = newTextIndex + 1
            If flag Then
                If IsNumeric(ch) Then
                    ch = exponentChars(ch)
                Else
                    flag = False
                End If
            End If

            newText(newTextIndex) = ch
        End If
    Next

    convertExponentsToSuperScript = Join(newText, "")
End Function


Test Code

Sub TestConvertExponentsToSuperScript()
    Dim target As Range
    Dim results As Variant
    Set target = Selection ' getTestRange(10000)
    results = target.value

    Dim t As Double: t = Timer

    Dim r As Long
    For r = 1 To UBound(results)
        results(r, 1) = convertExponentsToSuperScript(CStr(results(r, 1)))
    Next

    target.value = results

    Debug.Print Round(Timer - t, 2)
End Sub

Function getTestRange(TestRows As Long) As Range
    Dim results() As String
    ReDim results(1 To TestRows, 1 To 1)
    Dim r As Long
    For r = 1 To UBound(results)
        results(r, 1) = "O2 Permeability [cm^" &amp; WorksheetFunction.RandBetween(0, 1000) &amp; "/m^" &amp; WorksheetFunction.RandBetween(0, 1000) &amp; "]"
    Next
    Application.ScreenUpdating = False

    With Worksheets.Add
        .Range("A1") = "Test Data"
        With .Range("A2").Resize(UBound(results))
            .value = results
            Set getTestRange = .Cells
        End With
    End With
End Function