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.

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

Determining if Caps Lock is On

If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...

Discover More

Using the Drawing Grid

One of the lesser-known drawing tools provided in Word is the drawing grid. You can easily turn this feature on and use it to ...

Discover More

Workaround for Multiple Rows of Worksheet Tabs

If you've got a lot of worksheets in your workbook, you may want to display their tabs in to rows at the bottom of the Excel ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Replacing in Worksheets and Comments At the Same Time

If you need to replace information that may appear in cells, comments, and text boxes, your best bet is to use a macro. This ...

Discover More

Superscripts in Find and Replace

The find and replace used in Excel is less powerful than its counterpart in Word, so it is not able to do some of the ...

Discover More

Finding All Instances of a Value

Searching for information in an Excel worksheet generally goes very smoothly. There can be times, however, when the ...

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 6 - 0?

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.