Evan has a worksheet that contains a number of chemical compound notations, such as H2O. When he first enters the compound, he can format the 2 as a subscript, as it should be. However, Evan needs to search for instances where the 2 is not subscripted and replace it with the correctly subscripted notation. He wonders if there is an easy way to do this.
There is no way to do this using the Find and Replace features of Excel. The reason? As one ExcelTips subscriber put it, "Excel is spectacularly bad at handling rich text in cells." Evidence of this is the very fact that you cannot search for mixed formatting within a cell or replace with mixed formatting.
Note that I said "mixed formatting," which is what Evan wants—the "H" and "O" use different formatting than the "2". If Evan had wanted to change the entire cell contents to regular text or to superscript, then you could have used the regular Find and Replace tool. It won't work for mixed formatting, though.
This means that the best approach is to use a macro to do the finding and replacing. There are several ways you can approach this; the following is just one.
Sub SubscriptNumbers()
Dim c As Range
Dim sWord As String
Dim sChar As String
Dim x As Long
For Each c In Selection
sWord = c.Value
For x = 1 To Len(sWord)
sChar = Mid(sWord, x, 1)
If sChar >= "0" And sChar <= "9" Then
c.Characters(Start:=x, Length:=1).Font _
.Subscript = True
End If
Next x
Next c
Set c = Nothing
End Sub
To use the macro, select the cells you want to modify and then run it. It steps through each cell in the selection and then examines the cell contents. If there are any digits in the contents, then those digits are formatted as subscript.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13362) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
Need to get a count of a particular result from a formula? You can use Find and Replace (as described in this tip), but ...
Discover MoreTired of the Find and Replace dialog box blocking the view of your worksheet when you are searching for information? Do ...
Discover MoreYou can use Find and Replace as a quick way to count any number of matches in your document. You cannot, however, use it ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2026-08-11 10:17:53
J. Woolley
The Tip's SubscriptNumbers macro processes all cells in Selection containing formulas or constants, but any changes made to formula results are ignored because Range.Characters(Start, Length) only applies to constants (text, numeric, or logical). The macro is not restricted to text constants; therefore, numeric constants in Selection will remain numeric but with subscript font. If Selection contains an error value (like #N/A), the macro halts and displays an error dialog.
Here's an alternate version that resolves these issues by focusing on text constants:
Sub SubscriptNumbers2()
    Dim c As Range, sWord As String, n As Integer
    For Each c In Selection
        If VarType(c.Value) = vbString And Not c.HasFormula Then
            sWord = c.Value
            For n = 1 To Len(sWord)
                If Mid(sWord, n, 1) Like "#" Then
                    c.Characters(Start:=n, Length:=1).Font.Subscript = True
                End If
            Next n
        End If
    Next c
End Sub
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments