Finding and Replacing with Subscripts

by Allen Wyatt
(last updated January 24, 2015)

9

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 hand 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, and 2013.

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

Finding Long Sentences

For certain types of writing, you may want to make sure that the sentences in your document do not exceed a certain ...

Discover More

Checking Up On Numbers

When do you use digits in your prose and when do you spell out the numbers? Why not let Word help you make the decision? ...

Discover More

Keeping Centered Headers and Footers Centered

Headers and footers provide a nice final touch for your printed documents. If you want to expertly align text in those ...

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)

Limitations On Finding Characters

When you search for information in a worksheet, you expect Excel to return results that make sense. If you don't get a ...

Discover More

Getting a List of Matching Cells

The Find and Replace capabilities of Excel allow you to easily locate all the cells in a worksheet that contain specific ...

Discover More

Finding and Replacing in Text Boxes

Finding and replacing information in a worksheet is easy. Finding and replacing in other objects (such as text boxes or ...

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}] 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 four more than 2?

2017-01-03 12:29:57

Sara

OK... I use the subscript option... But when I close the document and re-open it tho computer applies a pre-determined format from nowhere.


2015-01-29 12:47:16

Michael (Micky) Avidan

@Willy,
As you probably know Macros do not scare me.
The problem arises with the majority of Excel users who have no previous VBA experience.
Thanks for the Chemical explanation.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2015-01-28 08:07:23

Willy Vanhaelen

@Micky
OK "Segoe UI Symbol" has it but it's still not easy to use. I would prefer to simply type the formula with normal numbers and run the macro.

BTW: H2SO4 is sulfuric acid. The stinky gas is hydrogen sulfide: H2S :-)


2015-01-27 13:30:58

Michael (Micky) Avidan

@Willy,
The only fact I recall, from Chemical lessons is that H2SO4 was a stinky(!) mixture.
"Segoe UI Symbol" font has ALL 10(!) subscript digits and most used (if not all) subscript characters.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2015-01-27 07:32:25

Willy Vanhaelen

@Micky

Let's not focus on that subscript 2. That's just an example (H2O) used in this tip. Chemical formulas use any subscript number such as H2SO4.

As far as I know there is no font that has the alphabet and all the subscript numbers. So we are stick with mixed formatting in Excel. You can do it manually which is rather tedious or with a macro. Your suggestion of a QAT button assigned to the macro is interesting if you use it a lot.


2015-01-26 09:02:25

Michael (Micky) Avidan

@Willy,
In such cases I mainly use the "Segoe UI Symbol" font.
To my opinion it is the richest font character.
This is, ofcourse for the Search&Replace natter.
If one uses a lot of "2 Subscrip" I would suggest to add a dedicated macro button to the QAT bar.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2015-01-26 05:53:45

Willy Vanhaelen

@Rob
I don't see any subscript numbers in standard fonts of Character Map. Only 1, 2 and 3 superscript.


2015-01-26 04:15:48

Rob

The easiest way is to not use a “2” and format as a subscript but to use the Windows program "Character Map" to find the correct character for a “subscript 2”. This may be specific to the character set being used (other subscript numbers exist, but some character sets may not have subscripts available).

This can be inserted into the string of the compound you need.

Once the correct character string for the compound is created, use This as the replace string in a standard Find & Replace.


2015-01-24 06:42:36

Willy Vanhaelen

Here is a shorter version of the macro:

Sub SubscriptNumbers()
Dim c As Range, x As Long
For Each c In Selection
For x = 1 To Len(c)
If IsNumeric(Mid(c, x, 1)) Then c.Characters(x, 1).Font.Subscript = True
Next x
Next c
End Sub


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.