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.
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.
Note:
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.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Macros are great for processing large amounts of data quickly. This tip examines several ways you can remove specific ...
Discover MoreExcel worksheets can contain all sorts of data. One thing you might store in a worksheet is a range of Boolean (TRUE or ...
Discover MoreWhen doing searches in Excel, you can use wildcard characters in the specification of what you are searching. However, ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-04-22 02:42:22
King
Following is the solution I came up with to remove prefix apostrophes while preserving cell formatting (there may be additional formatting that you need to add, but these did the job for me on Excel 365):-
Sub Remove_Prefix_Apostrophe()
'REMOVES PREFIX CHARACTER THAT APPEARS IN FORMULA BAR, BUT IS NOT PART OF THE FORMULA
'ENSURE TRANSITION NAVIGATION KEYS IS OFF
Application.TransitionNavigKeys = False
'FOR EACH CELL IN SELECTION
For Each CCELL In Selection.Cells
'IF PREFIX IS APOSTROPHE
If CCELL.PrefixCharacter = "'" Then
'STORE CURRENT CELL VALUE
CVAL = CCELL.Value
'STORE CURRENT CELL FONT & BORDER FORMATS
CBOLD = CCELL.Font.Bold
CITALIC = CCELL.Font.Italic
CINDENT = CCELL.IndentLevel
CBORTOPWT = CCELL.Borders(xlTop).Weight
CBORTOPSTY = CCELL.Borders(xlTop).LineStyle
CBORBOTWT = CCELL.Borders(xlBottom).Weight
CBORBOTSTY = CCELL.Borders(xlBottom).LineStyle
CBORLEFTWT = CCELL.Borders(xlLeft).Weight
CBORLEFTSTY = CCELL.Borders(xlLeft).LineStyle
CBORRIGHTWT = CCELL.Borders(xlRight).Weight
CBORRIGHTTSTY = CCELL.Borders(xlRight).LineStyle
'CLEAR THE CELL CONTENTS & FORMATTING
CCELL.Clear
'RESTORE THE CELL VALUE (WITHOUT THE APOSTROPHE)
CCELL.Value = CVAL
'RESTORE CELL FONT & BORDER FORMATS
CCELL.Font.Bold = CBOLD
CCELL.Font.Italic = CITALIC
CCELL.IndentLevel = CINDENT
CCELL.Borders(xlTop).Weight = CBORTOPWT
CCELL.Borders(xlTop).LineStyle = CBORTOPSTY
CCELL.Borders(xlBottom).Weight = CBORBOTWT
CCELL.Borders(xlBottom).LineStyle = CBORBOTSTY
CCELL.Borders(xlLeft).Weight = CBORLEFTWT
CCELL.Borders(xlLeft).LineStyle = CBORLEFTSTY
CCELL.Borders(xlRight).Weight = CBORRIGHTWT
CCELL.Borders(xlRight).LineStyle = CBORRIGHTTSTY
End If
Next CCELL
End Sub
2020-04-21 14:31:13
King
Neither the VB code method nor the PasteSpecial method work in Excel in Office 365: The apostrophe prefix remains but cell formatting is lost.
Any solution for 365 that doesn't lose cell formatting?
2014-05-09 05:03:50
Andreas H
Mjenkins
Yes you are right, "my" solution doesn't work on text-strings with letters in it, only numbers stored as text.
2014-05-08 10:01:31
MJenkins
Andreas H
You are right - it works great for numbers with the leading apostrophe, but it doesn't seem to work for the other example Allen gave i.e. 'a34tp. I had to convert an old company phone directory - all entries stored with a leading apostrophe. This is simple, but worked well for me:
1) Select the column with the data containing leading apostrophes
2) Go to a new area of the worksheet, or new worksheet
3) From the Paste menu, choose Paste Values
4) All apostrophes gone!
2014-05-08 06:32:01
Michael (Micky) Avidan
It seems as I didn't understand the Task/Question.
The linked picture shows the results of "Find & Replace" applied on cells B1:B9 which, at the beginning, resembled to cells A1:A9.
http://jpg.co.il/download/536b5b5ea60fa.png
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL
2014-05-08 05:34:22
Andreas H
MJenkins
I just tested it again in excel 2007, it worked with a charm. ex:
1. write '1111 in a cell
2. select cell or cells
3. select "Text to coloums" in the "Data tools"-set
4. choose delimited
5. press next
6. make sure no boxes is checked (not really important)
7. press finish
The result, the leading ' is gone, and the numbers are recognized as numbers by excel.
2014-05-07 17:09:55
David Gardner
Allen's code works provided you've done the following with the code:
Add a new module to your workbook if none exists.
Put the code in the module.
The code could look something like this:
Sub RemovePrefixChars()
Application.TransitionNavigKeys = False
For Each c In Selection
If c.PrefixCharacter = "'" Then
c.Value = c.Value
End If
Next c
Application.TransitionNavigKeys = True
End Sub
The code expects you to select the cells that have the prefix characters before you run it.
This worked in Excel 2010 where I typed some numbers in a cell prefixed with the apostrophe. I copied that cell to a few cells below it, selected them, and then ran the code. I could tell that the code works, because the 'text' was aligned to the left in the cells, and then after the code ran, the numbers were aligned to the right.
2014-05-07 09:31:21
MJenkins
Andreas, I couldn't get your tip to work in Excel 2007. Am I missing something?
2014-05-07 08:23:52
Andreas H
If you want to clear this leading apostrophe from a coloum, you can use the "text-to-coloum"-tool.
Select the coloum, (or a part of it) choose delimited and uncheck all the "delimitation-options", click finish and you should be good to go!
2013-12-27 15:27:46
Upsilon
The macro loop in the ExcelTip is not preserving data when an apostrophe is the leading character. The leading apostrophe is migrated to PrefixCharacter, and the data in the cell is reduced by one character. Although it is possible to use Copy/PasteSpecial,Values and other processes such as TextToColumns and QueryTables to force a condition where PrefixCharacter is a null string and there is a leading apostrophe in the data; doing so is unstable, as pressing F2 or double-clicking the cell will migrate the leading apostrophe to PrefixCharacter. Here is a loop that preserves the data, tested on Excel 2007.
Sub clearPrefixCharacter()
Dim rCell as Range
Application.TransitionNavigKeys = False
For Each rCell In Selection
If rCell.PrefixCharacter = "'" Then
If Left(rCell.Value, 1) <> "'" Then
rCell.Value = rCell.Value
End If
End If
Next rCell
End Sub
I think that Microsoft should provide a rawText format that will not interpret data.
2012-11-21 11:51:42
Dave Unger
Thanks Lou, that was the answer I was looking for. Dave
2012-11-20 09:24:35
Lou
Excel 2010 does seem to play by different rules. Once you put a prefix character on a cell, as long as there is a text value in the cell, the prefix character persists. The only luck I've had in getting rid of them is to use the Format Painter to remove the persistent prefix. Select an "uninfected" cell, and paste the format to the desired cell.
The VBA Code to fix cell A2:
Range("AI65000").Copy
Range("A2").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
2012-10-10 14:33:02
James
I have the same problem with Excel 2010. I would like to thank you for a generally well written article, I feel like I understand it much better now. As someone who has never had VBA training, I would appreciate the lead-in to the code to show what is necessary to run it. [i.e. Dim c as Range, etc...b/c I borrowed that snipet from previously attempted code.] Thanks.
2011-12-01 01:32:44
Hi Allen,
Thanks for this great tip, exactly what I was looking for. The Copy/PasteSpecial method works well on all my 3 versions (2003/2007/2010). As does the macro in 2003/2007, but seems to have no effect in xl2010 (which is what I need, of course!). I've checked over everything carefully, even re-installed xl2010, but to no avail. Do you have any suggestions? Possibly an option setting, but haven't discovered it so far. Thank you,
regards,
Dave
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.
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