Finding Positions of Formatted Characters in a Cell

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


Gary has a worksheet that contains unstructured text strings that he needs to parse into component elements. Sometimes he needs to search for types of text rather than a specific character. For example, he might want to find the first, the Nth, or the last occurrence of any bold character (or any italics character) in the cell.

There is no intrinsic function or tool in Excel to accomplish this task. The regular Find and Replace feature returns matches for formatting, but not the position at which those matches occur. To get that specific about what you want to find, you'll need to use a macro. You can get the positioning information back into your worksheet if you implement the macro as a user-defined function.

The following macro accepts a range (intended to be a specific cell), an indicator of whether you want bold or italic (or both), and the occurrence of that formatting.

Function FindNth(r As Range, sType As String, N As Integer) As Integer
    Dim J As Integer
    Dim iCount As Integer
    Dim sStyle As String
    
    If r.Count = 1 Then
        FindNth = 0
        iCount = 0
        For J = 1 To Len(r.Text)
            sStyle = r.Characters(J, 1).Font.FontStyle
            If LCase(sStyle) = LCase(sType) Then
                iCount = iCount + 1
                If N = 0 Then
                    FindNth = J
                Else
                    If N = iCount Then
                        FindNth = J
                        Exit For
                    End If
                End If
            End If
        Next J
    Else
        FindNth = -1
    End If
End Function

In order to use the macro, use one of the following formulas in your worksheet:

=FindNth(A1, "bold", 2)
=FindNth(A1, "italic", 3)
=FindNth(A1, "bold italic", 1)

In each case the third parameter specifies which occurrence of the given formatting you want to find. The function returns the character position of that occurrence within the cell. If there is no such occurrence, then 0 is returned. If you specify multiple cells in the first parameter of the function, it returns a -1. If you specify an occurrence of 0, then the character position of the last occurrence of the specified format is returned.

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 (13402) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Understanding Auditing

Excel provides some great tools that can help you see the relationships between the formulas in your worksheets. These ...

Discover More

Allowing Only Form Field Changes

Word allows you to create forms that other people can use to enter information. One of the last steps normally taken with ...

Discover More

Setting Consistent Column Widths in Multiple Tables

Tables are great for organizing and presenting information in a document. If you have a document containing multiple ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Importing Based on a Partial File Name

A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...

Discover More

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come ...

Discover More

Sheets for Months

One common type of workbook used in offices is one that contains a single worksheet for each month of the year. If you ...

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 seven more than 1?

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.