Finding and Replacing with Subscripts

Written by Allen Wyatt (last updated September 19, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Evan has a worksheet that contains a number of chemical compound notations, such as H2O. When he firsts 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 subscribe, 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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13362) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of Custom Toolbars

If you start Word and get a bunch of unwanted toolbars displayed on the screen, you can be scratching your head (or ...

Discover More

Diagonal Borders in a Conditional Format

Conditional formatting is a great tool for changing how your data looks based on the data itself. Excel won't allow you ...

Discover More

Choosing a Power Management Plan

Windows maintains a number of power management plans that define how long the various components of your system sit idle ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Replacing Characters at the End of a Cell

The Find and Replace capabilities of Excel can come in handy, but they can't accomplish all your replacement needs. One ...

Discover More

Changing the Color Used to Highlight Found Information

When you want to find information in worksheet, Excel can handily locate and highlight that information. If you find the ...

Discover More

Searching for Leading Apostrophes

Take a look at the Formula bar when you select a cell that contains text, and you may see an apostrophe at the beginning ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.