Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Using Find and Replace to Pre-Pend Characters

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.

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

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.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

jraju    21 Nov 2012, 08:28
Great tip sir. I have gone to the trust centre in the excel options in ms excel 2007, and disabled the connections for my computer. But I do not know, how to find the hidden macro , if any, resident sir. When i click the existing connection, there was one connection dated very old.So, i removed all those, and now i did not get the macro warning.
Barry Fitzpatrick    19 Nov 2012, 06:46
In all versions of Excel you can bring up the Macro editor (better known as the Visual Basic Editor, VBE) by pressing Alt+F11.

If macros are resident in a Module or Userform then to avoid the macros warning the Module/Userform itself must also be deleted as well as any resident macros.

You could also put the workbook into a "trusted" location and run form there PROVIDED you are completely comfortable that the macros are not harmful.
jraju    19 Nov 2012, 05:55
Great tip barry sir, This is a perfect point to point practical reply.
jraju    19 Nov 2012, 05:53
Great tip barry sir, This is a perfect point to point practical reply. By the by, may ask one other sub question.I do not find any macro ( i copied the webcontents to excel , omitting hyperlinks etc). But when i open the excel book, I get the macro warning. How to find the macro in excel 2007 and to delete it? I fear loss of data if i disable the macro,if present. Thank you, sir, once again for this excellent tip. It worked for me. I rate this reply highly and would recommend to be added to the FAQ
Barry Fitzpatrick    18 Nov 2012, 07:54
jraju

The problem you are seeing is due to the fact the time is stored as an Excel date/time serial number, then the formatting is showing it as a time.

There is a couple of approaches you can take:
1. use custom formatting, and preserve the underlying value as a time value. the easiest way to do this is to open the cell formatting dialog box (Ctrl+1), click on "Custom" then insert "C" to the formatting in the "Type:" box, then click OK;
2. in an adjacent cell converting the time to a text string and prefix the text string with "C". e.g. if the time is in cell A1 the the formula "="C"&Text(A1,"h:mm AM/PM")) will give the desire result. OR
if your using the macros in the tip replace the formula: c.value=ToPrepend &c.value with:
c.value=ToPrepend & Format(c.value,"h:mm AM/PM")

The first method is I believe the best as it preserves the underlying time value.
jraju    18 Nov 2012, 04:29
When i tried to concatenate the letter "C" with the h:mm format (3:55:00 A.M) the result is not expected A3:55 but C0.163194444444444 . Why? Is there a solution. can this 3:55:00 A.M be made
:55
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.