does anyone know how to encode subscripts?

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
I need to write things like H2O in the Excel but 2 of course being subcripted. Does anyone know how to code for that in VBA?
 
Here is my take on coding for this question (it does like shg's code and marks improperly formed text yellow)...

Code:
Sub SubscriptBetweenBackwardForwardSlashes()
  Dim X As Long, R As Long, C As Long, BS As Long, FS As Long, Data As Variant, Temp As String
  If ActiveSheet.UsedRange.Address(0, 0) = "A1" And Len(Range("A1").Value) = 0 Then Exit Sub
  Data = Range("A1").Resize(Cells.Find("*", , xlValues, , xlRows, xlPrevious).Offset(1).Row, _
         Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Offset(, 1).Column)
  Application.ScreenUpdating = False
  For R = 1 To UBound(Data)
    For C = 1 To UBound(Data, 2)
      [COLOR=#0000FF][B]If Len(Data(R, C)) Then[/B][/COLOR]
        Temp = Data(R, C)
        For X = 1 To Len(Temp)
          If Mid(Temp, X, 1) Like "[!/\]" Then Mid(Temp, X, 1) = " "
        Next
        Temp = Replace(Temp, " ", "")
        If Temp Like "*//*" Or Temp Like "*\\*" Or UBound(Split(Temp, "/")) <> UBound(Split(Temp, "\")) Then
          Cells(R, C).Interior.ColorIndex = 6
        Else
          FS = InStrRev(Cells(R, C).Value, "/")
          Do While FS
            BS = InStr(FS, Cells(R, C).Value, "\")
            Cells(R, C).Characters(FS, BS - FS + 1).Text = Cells(R, C).Characters(FS + 1, BS - FS - 1).Text
            Cells(R, C).Characters(FS, BS - FS - 1).Font.Subscript = True
            FS = InStrRev(Cells(R, C).Value, "/", FS - 1)
          Loop
        End If
      End If
    Next
  Next
  Application.ScreenUpdating = True
End Sub
One small change which should (untested) make the code ever so slightly faster...
Code:
Sub SuperScriptBetweenBackwardForwardSlashes()
  Dim X As Long, R As Long, C As Long, BS As Long, FS As Long, Data As Variant, Temp As String
  If ActiveSheet.UsedRange.Address(0, 0) = "A1" And Len(Range("A1").Value) = 0 Then Exit Sub
  Data = Range("A1").Resize(Cells.Find("*", , xlValues, , xlRows, xlPrevious).Offset(1).Row, _
         Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Offset(, 1).Column)
  Application.ScreenUpdating = False
  For R = 1 To UBound(Data)
    For C = 1 To UBound(Data, 2)
      [B][COLOR=#0000FF]If Data(R, C) Like "*[/\]*" Then[/COLOR][/B]
        Temp = Data(R, C)
        For X = 1 To Len(Temp)
          If Mid(Temp, X, 1) Like "[!/\]" Then Mid(Temp, X, 1) = " "
        Next
        Temp = Replace(Temp, " ", "")
        If Temp Like "*//*" Or Temp Like "*\\*" Or UBound(Split(Temp, "/")) <> UBound(Split(Temp, "\")) Then
          Cells(R, C).Interior.ColorIndex = 6
        Else
          FS = InStrRev(Cells(R, C).Value, "/")
          Do While FS
            BS = InStr(FS, Cells(R, C).Value, "\")
            Cells(R, C).Characters(FS, BS - FS + 1).Text = Cells(R, C).Characters(FS + 1, BS - FS - 1).Text
            Cells(R, C).Characters(FS, BS - FS - 1).Font.Subscript = True
            FS = InStrRev(Cells(R, C).Value, "/", FS - 1)
          Loop
        End If
      End If
    Next
  Next
  Application.ScreenUpdating = True
End Sub[COLOR=#0000FF][/COLOR]
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
One small change which should (untested) make the code ever so slightly faster...
Code:
Sub SuperScriptBetweenBackwardForwardSlashes()
  Dim X As Long, R As Long, C As Long, BS As Long, FS As Long, Data As Variant, Temp As String
  If ActiveSheet.UsedRange.Address(0, 0) = "A1" And Len(Range("A1").Value) = 0 Then Exit Sub
  Data = Range("A1").Resize(Cells.Find("*", , xlValues, , xlRows, xlPrevious).Offset(1).Row, _
         Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Offset(, 1).Column)
  Application.ScreenUpdating = False
  For R = 1 To UBound(Data)
    For C = 1 To UBound(Data, 2)
      [COLOR=#000000]If Data(R, C) Like "*[/\]*" Then[/COLOR]
        Temp = Data(R, C)
        For X = 1 To Len(Temp)
          If Mid(Temp, X, 1) Like "[!/\]" Then Mid(Temp, X, 1) = " "
        Next
        Temp = Replace(Temp, " ", "")
        If Temp Like "*//*" Or Temp Like "*\\*" Or UBound(Split(Temp, "/")) <> UBound(Split(Temp, "\")) Then
          Cells(R, C).Interior.ColorIndex = 6
        Else
          FS = InStrRev(Cells(R, C).Value, "/")
          Do While FS
            BS = InStr(FS, Cells(R, C).Value, "\")
            Cells(R, C).Characters(FS, BS - FS + 1).Text = Cells(R, C).Characters(FS + 1, BS - FS - 1).Text
            Cells(R, C).Characters(FS, BS - FS - 1).Font.Subscript = True
            FS = InStrRev(Cells(R, C).Value, "/", FS - 1)
          Loop
        End If
      End If
    Next
  Next
  Application.ScreenUpdating = True
End Sub
Not wanting to give up on this question, here is a modification that will allow natural forward and/or backward slashes to be preserved while still letting them be used as delimiters for the subscripting. The method is to do what VB and Excel do for internal quote marks... double them up and after processing they will be converted to normal forward or backward slashes (that is use two adjacet forward or two adjacent backward slashes in the text where you want a single forward or backward slash to appear and that will happen after the code is run).
Code:
Sub SuperScriptBetweenBackwardForwardSlashes()
  Dim X As Long, R As Long, C As Long, BS As Long, FS As Long, Data As Variant, Temp As String
  If ActiveSheet.UsedRange.Address(0, 0) = "A1" And Len(Range("A1").Value) = 0 Then Exit Sub
  Application.ScreenUpdating = False
[COLOR=#0000FF][B]  ActiveSheet.UsedRange.Replace "//", Chr$(2), xlPart
  ActiveSheet.UsedRange.Replace "\\", Chr$(3), xlPart[/B][/COLOR]
  Data = Range("A1").Resize(Cells.Find("*", , xlValues, , xlRows, xlPrevious).Offset(1).Row, _
         Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Offset(, 1).Column)
  For R = 1 To UBound(Data)
    For C = 1 To UBound(Data, 2)
      If Data(R, C) Like "*[/\[B][COLOR=#0000FF]" & Chr$(2) & Chr$(3) & "[/COLOR][/B]]*" Then
        Temp = Data(R, C)
        For X = 1 To Len(Temp)
          If Mid(Temp, X, 1) Like "[!/\]" Then Mid(Temp, X, 1) = " "
        Next
        Temp = Replace(Temp, " ", "")
        If Temp Like "*//*" Or Temp Like "*\\*" Or UBound(Split(Temp, "/")) <> UBound(Split(Temp, "\")) Then
          Cells(R, C).Interior.ColorIndex = 6
        Else
          FS = InStrRev(Cells(R, C).Value, "/")
          Do While FS
            BS = InStr(FS, Cells(R, C).Value, "\")
            Cells(R, C).Characters(FS, BS - FS + 1).Text = Cells(R, C).Characters(FS + 1, BS - FS - 1).Text
            Cells(R, C).Characters(FS, BS - FS - 1).Font.Subscript = True
            FS = InStrRev(Cells(R, C).Value, "/", FS - 1)
          Loop
[COLOR=#0000FF][B]          If Cells(R, C).Value Like "*[" & Chr$(2) & Chr$(3) & "]*" Then
            For X = 1 To Len(Cells(R, C).Value)
              If Mid(Cells(R, C).Value, X, 1) Like Chr$(2) Then
                Cells(R, C).Characters(X, 1).Text = "/"
              ElseIf Mid(Cells(R, C).Value, X, 1) Like Chr$(3) Then
                Cells(R, C).Characters(X, 1).Text = "\"
              End If
            Next
          End If[/B][/COLOR]
        End If
      End If
    Next
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,332
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top