Written by Allen Wyatt (last updated December 8, 2021)
This tip applies to Excel 2007, 2010, 2013, and 2016
Brenda has a lot of information that has been imported or pasted into a worksheet. Sometimes the text in the worksheet will contain "foreign" and strange characters. She wonders if there is a way to easily convert the data so that it contains no non-ASCII characters and, perhaps, some foreign characters are converted to regular ASCII values (such as converting accented letters to non-accented letters).
There are a couple of things you can try. First, you can use the CLEAN worksheet function to get rid of non-printable characters. Just use the function in this manner:
=CLEAN(A1)
The result is "cleaned" text, without the non-printables. If you want to replace foreign characters with regular ASCII characters, that will need to be done with a macro. Here's an example of a relatively straightforward approach:
Sub StripAccent() Dim sAcc As String Dim sReg As String Dim sA As String Dim sR As String Dim i As Integer sAcc = "ŠŽšžŸÀÁÂÃÄ�...ÇÈÉÊËÌÍÎÏÐ�'�'�"�"ÕÖÙÚÛÜÝàáâãäåçè�©êëìí�®ïðñòóôõöùúûüýÿ" sReg = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy" For i = 1 To Len(sAcc) sA = Mid(sAcc, i, 1) sR = Mid(sReg, i, 1) Selection.Replace What:=sA, Replacement:=sR, _ LookAt:=xlPart, MatchCase:=True Next End Sub
The macro steps through the characters in the sAcc variable and, one at a time, uses Find and Replace to replace them with the corresponding character in the sReg variable. You can adjust the contents of sAcc and sReg to reflect your conversion needs; the key is to make sure that they are both the same length.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11493) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Converting to ASCII Text.
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!
Need to get rid of everything in a worksheet except the formulas? It's easier to make this huge change than you think it is.
Discover MoreImporting data into Excel that was generated in other programs can have some interesting side effects. For instance, you ...
Discover MoreWant a really easy way to create a selection of a group of cells? Discover how to use the Extend key to make this task ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-12-10 11:54:10
BJThomasJr
I checked the comments to gather helpful info and noticed some opening comments stating:
"The macro in this tip is really sloppy work" "Obviously..." "a dozen of useless characters" "my version of the macro (smaler as usual :-) that does the job properly"
While the opinions may be true, does the opening sentence often set a "tone" and "expectation" for the reader?
Because for me, I think of conflict, attacks and argumentative posturing however all I really want is well presented information.
Please make your points more inclusive to allow many audiences to share and learn from them... even for timid, uneducated readers like myself?
Respectfully, BJT
2021-12-08 10:21:25
Willy Vanhaelen
I'm perplexed! The text of this tip starts with (last updated June 19, 2018) but Allen or his co-worker didn't take any notice of my comment of 26 Aug 2017 reporting a severe bug in the macro, it still fails to work.
Today i got a "Daily Nugget" referring to this tip with still the cripple macro... That's bad publicity !
2017-08-29 12:17:06
Willy Vanhaelen
@Ru Lopi
If you want to do it with a UDF this smaler function will do it much faster since it has only one loop:
Function StripAccent(sText As String) As String
Dim X As Integer, Y As Integer
Const sAcc = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÓÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
Const sReg = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
For X = 1 To Len(sText)
Y = InStr(sAcc, Mid(sText, X, 1))
If Y Then Mid(sText, X, 1) = Mid(sReg, Y, 1)
Next X
StripAccent = sText
End Function
2017-08-28 08:40:55
Ru Lopi
The following will replace foreign characters in a cell.
In a module place this code:
Function funcStripAccent(strText As String) As String
Dim sAcc As String
Dim sReg As String
Dim intInner As Integer
Dim intOuter As Integer
sAcc = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÓÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
sReg = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
For intOuter = 1 To Len(strText)
For intInner = 1 To Len(sAcc)
If Mid(strText, intOuter, 1) = Mid(sAcc, intInner, 1) Then
Mid(strText, intOuter, 1) = Mid(sReg, intInner, 1)
End If
Next intInner
Next intOuter
funcStripAccent = strText
End Function
Assuming that the foreign text is in A1 then in the destination cell enter: =funcStripAccent(A1)
This can be fllled to other cells as required.
As mentioned, sAcc and sReg must be the same length with matching conversion characters.
2017-08-26 14:30:11
Willy Vanhaelen
The macro in this tip is really sloppy work. When you copy and paste it in VBA the "sAcc = ..." row gets entirely blood red meaning there is an error. This is because at 2 places in the string there is a single quotation mark which is not allowed . When you remove them you can run the macro but it replaces all the characters in the selected text with an o (ooooooo...)!!!.
Obviously the sAcc and sReg strings should be exactly the same length which is not the case here. The sAcc string is much longer and contains beside the 2 quotation marks a dozen of useless characters including some question marks. These causes the erroneous result. The last question mark in the sAcc string corresponds to the letter o in the sReg string so when sA contains a ? , Replace considers this as a wild card and replaces all the characters in the selection with an o which is the content of sR at that moment.
Here is my version of the macro (smaler as usual :-) that does the job properly:
Sub StripAccent()
Const sAcc = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçéèêëìíîïðñòóôõöùúûüýÿ"
Const sReg = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeeiiionooooouuuuyy"
Dim X As Integer
For X = 1 To Len(sAcc)
Selection.Replace _
What:=Mid(sAcc, X, 1), _
Replacement:=Mid(sReg, X, 1), _
LookAt:=xlPart, MatchCase:=True
Next
End Sub
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 © 2023 Sharon Parq Associates, Inc.
Comments