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.
Written by Allen Wyatt (last updated January 12, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
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.
Note:
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.
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!
If you have a folder that contains dozens or hundreds of workbooks, you may need to search through those workbooks to ...
Discover MoreThe Find and Replace capabilities of Excel can come in handy, but they can't accomplish all your replacement needs. One ...
Discover MoreExcel provides some great tools for finding information in a worksheet or a workbook. Changing the default settings used ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-01-30 11:32:08
ionut
I wanted just to eliminate those apostrophs signs, I have copied the text into a text file and pasted back into Excel. The apostroph are gone.
2018-08-31 16:58:30
Brian
Thank you for this trick - it saved me a lot of time recently. However, today I tried doing it, and Excel now adds an apostrophe prefix to the cell contents after the 2nd step (replacing = with #= ; Excel now replaces it with '#= ). Any idea what might have caused this change in behavior?
2018-01-23 16:14:46
Allen,
Thank you for this solution. It is exactly what I needed to save me several minutes of work today. I am grateful to you for your ongoing support of the Excel community. I had been a long-time subscriber to your weekly newsletter, but in these waning years of my current situation, I'm cutting back on some of the inbox load and kind of limiting my self to communications from the Treacys and Jon Acampora. That doesn't mean, however, that I haven't benefited from your efforts and today was a reminder of that. Thanks and I wish you well, sir.
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