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

Inserting a Section Mark

Section marks are used regularly in the writings of some industries, such as in legal documents. If you need a way to ...

Discover More

Changing a Toolbar Button Image

Excel allows you to modify virtually all aspects of its user interface. One of the things you can change is the images ...

Discover More

Flipping a Drawing Object

Don't like the way a drawing object looks? Perhaps flipping the object could help appearances. Excel allows you to flip ...

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)

Wildcards in 'Replace With' Text

When doing searches in Excel, you can use wildcard characters in the specification of what you are searching. However, ...

Discover More

Replacing Only Whole Words in Excel

Excel's Find and Replace capabilities are handy, but they aren't as full-featured as those in Word. One shortcoming is ...

Discover More

Using Find and Replace to Find Conditionally Formatted Cells

You can use Find and Replace as a quick way to count any number of matches in your document. You cannot, however, use it ...

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 two more than 7?

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.