Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. 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: Searching for Leading Apostrophes.
by Allen Wyatt
(last updated September 29, 2014)
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 advanced area of the Excel Options dialog box (at the bottom of the 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 (10675) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Searching for Leading Apostrophes.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Using Find and Replace is something quite routine in Excel, as it easily allows you to find and replace information in ...Discover More
Excel worksheets can contain all sorts of data. One thing you might store in a worksheet is a range of Boolean (TRUE or ...Discover More
Need to add some characters to the beginning of the contents in a range of cells? It's not as easy as you might hope, but ...Discover More
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.