Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Using Find and Replace to Pre-Pend Characters.
Written by Allen Wyatt (last updated December 28, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3883) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Using Find and Replace to Pre-Pend Characters.
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!
Take a look at the Formula bar when you select a cell that contains text, and you may see an apostrophe at the beginning ...
Discover MoreIf you have a folder that contains dozens or hundreds of workbooks, you may need to search through those workbooks to ...
Discover MoreWhen you are working on a worksheet (particularly a large one), you may want to search for and possibly copy information ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments