Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using Find and Replace to Pre-Pend Characters.

Using Find and Replace to Pre-Pend Characters

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


1

Mel often wants to pre-pend a character to the beginning of whatever is in a range of cells. For instance, he may want to add a letter to the start of some text (so "123" becomes "A123" and "xyz" becomes "Axyz") or he may want to add an apostrophe (so "123" becomes "'123" and "xyz" becomes "'xyz"). Mel wonders if this can be done using Find and Replace.

The short answer is that it cannot. The Find and Replace capabilities in Excel are more limited than those in Word, where you have the capability to search for wildcards and use the "Find What" text in what is replaced. (These are just two examples of capabilities missing in Excel's Find and Replace.)

One potential answer, then, is to copy your data over to Word, use Find and Replace to make the changes, and then copy the data back. Of course, you run the risk of losing your formatting in the round trip, losing some of your precision, and converting all your formula results to static values. For many users, these are not acceptable risks.

Another option is to use the concatenation capabilities of Excel. For instance, if the values you want to pre-pend is in column A (beginning with A1), then you would use a formula such as this is column B:

="A" & A1

The result pre-pends the letter A to whatever is in A1. This works for pre-pending anything except an apostrophe. Trying to pre-pend an apostrophe ends up with '123 or 'xyz, but the apostrophe is visible in the cell. The result is not the same, to Excel, as typing an apostrophe followed by 123 or an apostrophe followed by xyz. (In the case of typing, the apostrophe indicates the cell contents should be treated as text and the apostrophe is only visible in the Formula bar, not in the cell itself.)

If you actually want to change the values in a series of cells (which a desire to use Find and Replace would suggest), then the only thing you can do is to use a macro to make your changes. If you only want to pre-pend cells beginning with a set value (such as 123) with a letter (such as A), then a simple macro will suffice.

Sub Prepend1()
    ToFind = "123"
    ToFindLength=Len(ToFind)
    ToPrepend = "A"

    For Each rcell In Selection
        If LCase(Mid(rcell.Value, 1, ToFindLength)) =  LCase(ToFind) Then
            rcell.Value = ToPrepend & rcell.Value
        End If
    Next
End Sub

Note that the ToFind variable contains the beginning text that you want to pre-pend and the ToPrepend variable contains what you want to appear before that string. In this instance, when you select a range of cells and run the macro, anything beginning with 123 (such as "123" or "12345" or "123D27X") will have the letter A added to the front of the cell.

Such a macro doesn't help, however, when you want to add the letter to the front of every cell in the range, not just those beginning with 123. In that case you need a different approach.

Sub Prepend2()
    Dim rng As Range
    Dim c As Range
    Dim ToPrepend As String

    ToPrepend = "A"

    ' Process only text and number constants
    Set rng = Selection.SpecialCells(xlCellTypeConstants, 3)

    For Each c In rng
        c.Value = ToPrepend & c.Value
    Next c
End Sub

This macro takes a subset of whatever cells you selected before running it (only those cells containing text and numeric values) and then adds the contents of the ToPrepend variable to the start of the cell. If you want to change what is pre-pended, simply change the value of the variable. (It should be noted that if you change ToPrepend to an apostrophe, then the cells to which the apostrophe is pre-pended behave exactly as if you had typed and apostrophe followed by the cell value.)

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 (12239) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Using Find and Replace to Pre-Pend Characters.

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

Changing Borders for Data Series

Microsoft Graph allows you to easily add charts to a Word document without using Excel. This tip explains how you can ...

Discover More

Splitting Information into Rows

Got too much information in a single cell? Here's how you can use a macro to pull apart that information and put it into ...

Discover More

Opening a Backup File

If you have Word configured to save backup copies of your document, you may want to actually load one of those copies at ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

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

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. ...

Discover More

Replacing Tildes at the Beginning of a Cell

Replacing a specific character (such as a tilde) seems a simple task, until you need to replace it only in a certain ...

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 four minus 0?

2022-08-06 08:47:34

Mike J

The 'hidden' apostrophe situation may be resolved by changing .value to .FormulaR1C1 in the prepend macros.

A simpler version, just for apostrophes is:

Sub Apostrophise()
For Each c In Selection
c.FormulaR1C1 = "'" & c.Value 'or c.value2
Next c
End Sub

And then to remove the 'hidden' apostrophes:

Sub UnApostrophise()
For Each c In Selection
c.Value = c.Value
Next c
End Sub

Obviously the formatting may need adjustment and there is no error (or data-type) checking, but it seems to work.


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.