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.
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.
Learn more about Allen...
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: Searching for Leading Apostrophes.
Richard would like to be able to search for an apostrophe (') in the leftmost position in a cell, but Excel won't let him do it. In other words, if a cell contains '123 or 'a34tp, Richard would like to be able to find that leading apostrophe and, optionally, replace it with something else.
Doing what Richard wants to do takes a bit of preliminary explaining. Technically, it is a misnomer to refer to the apostrophe as a "leading character" or mentioning that it is in the "leftmost position" of a cell. Even though you may be able to look at the Formula bar and see the apostrophe at the beginning of the formula, that apostrophe is not really a part of the cell's contents; that is why you can't use Find and Replace to find and replace it.
The apostrophe is actually considered a "prefix character" for a cell. The possible values of the prefix character are set by the Transition Navigation Keys setting in Excel, and the value of the setting is saved on a workbook-by-workbook basis. You can change this setting by using the Transition tab of the Options dialog box.
If the setting is cleared (the default condition for the setting), then the value of the prefix character for each cell can either be blank or an apostrophe. If the cell contains text, then the setting of the prefix character doesn't really matter much. If the cell contents are not text, then setting the prefix character to an apostrophe forces Excel to treat the cell contents as if they are text. So, for instance, the number 123 is treated as text—not a number—and shows up in the Formula bar as '123.
If the Transition Navigation Keys setting is selected (the check box has a check mark in it), then the value of the prefix character for each cell can have one of five different values. These values are consistent with the prefixes used in Lotus 1-2-3 and are, oddly enough, supported in Excel only as a transitional aid to the regular usage in the program. The possible values are an apostrophe (left-justified), quote mark (right-justified), carat (centered), back slash (repeated), or blank (non-text item).
Now, back to Richard's original question: how to search and get rid of that leading apostrophe. You can't use Find and Replace to do the editing because the apostrophe isn't really part of the cell contents. So, you must do the changing in a macro. The changing is relatively easy. First, you'll want to make sure that the workbook has the Transition Navigation Keys setting cleared. Why? Because you probably don't want to mess up the prefix character for the cells if the workbook could be used at some future point with Lotus 1-2-3 again. You make sure that the setting is correct, in your macro, with the following line:
Application.TransitionNavigKeys = False
You can then step through a selection of cells and check to see if the prefix character for each cell is an apostrophe. If it is, then all you need to do is have the macro do the equivalent of manually retyping the contents of the cell, in the following manner:
For Each c In Selection If c.PrefixCharacter = "'" Then c.Value = c.Value End If Next c
Note that the macro checks what is in the PrefixCharacter property. This property can be read in VBA, but it cannot be changed directly. That is why the macro needs to use the seemingly simple line to assign the value of each cell back into the cell—essentially retyping the contents.
If you would rather not use a macro to get rid of the apostrophe prefix characters, then you can take advantage of a strange little quirk of Paste Special. Follow these general steps:
After the "adding" of the blank cell to each of the target cells, the prefix character—if any—is removed.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3332) 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: Searching for Leading Apostrophes.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!