|
-
Aug 14th, 2007, 12:32 PM
#1
Thread Starter
Hyperactive Member
Excel - Replace accented chars with regular chars
Whats a good way for me to go through all the columns in a spread sheet and replace accented characters with non-accented characters?
-
Aug 14th, 2007, 04:44 PM
#2
Re: Excel - Replace accented chars with regular chars
you can do a loop for each cell in usedrange
then use replace to replace the characters you wish to replace,
there could be other solutions, but that is probably the simplest
you can nest replace to replace mutiple characters, but i am not sure how deep, so you might have to use multiple replace on each cell, depending how many accented characters you might need to replace
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 15th, 2007, 12:05 PM
#3
Thread Starter
Hyperactive Member
Re: Excel - Replace accented chars with regular chars
How can I use the replace function within the code? please post an example.
-
Aug 15th, 2007, 12:20 PM
#4
Thread Starter
Hyperactive Member
Re: Excel - Replace accented chars with regular chars
I tried this but its not working, it cannot find the sub or function 'Replace'
Code:
Dim col As Integer
col = TextBox1.Text
For Row = 2 To ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count
Dim theinput As String
theinput = ActiveSheet.Cells(Row, col)
Dim out As String
out = Replace(theinput, "A", "AAA")
ActiveSheet.Cells(Row, col) = out
Next Row
-
Aug 15th, 2007, 05:00 PM
#5
Re: Excel - Replace accented chars with regular chars
what version of excel?
replace does not exist in '97, but is in 2000 on
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 15th, 2007, 05:57 PM
#6
Thread Starter
Hyperactive Member
Re: Excel - Replace accented chars with regular chars
Im on Excel 2004 for Macs. It should work the same as 2000 vba wise.
-
Aug 15th, 2007, 10:21 PM
#7
Re: Excel - Replace accented chars with regular chars
just made a dedicated function for what you want
vb Code:
Function repaccent(inputstr As String)
mystr = "" 'put here the list of accented characters
myrepstr = "" 'put here the list of characters to replace them with in the same order (position is critical)
' both strings must be the same length
For i = 1 To Len(inputstr)
pos = InStr(mystr, Mid(inputstr, i, 1)) 'check if each character is in the accented list
If Not pos = 0 Then inputstr = Left(inputstr, i - 1) & Mid(myrepstr, pos, 1) & Mid(inputstr, i + 1)
' if it is replace it in the string with the equivalent character from the second string
Next
repaccent = inputstr
End Function
call it like
vb Code:
ActiveSheet.Cells(Row, col) = repaccent(ActiveSheet.Cells(Row, col))
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 16th, 2007, 09:00 AM
#8
Re: Excel - Replace accented chars with regular chars
Here you are. This function will work much better:
VB Code:
Option Explicit
'-- Add more chars to these 2 string as you want
'-- You may have problem with unicode chars that has code > 255
'-- such as some Vietnamese characters that are outside of ASCII code (0-255)
Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
Sub StripAccent(aRange As Range)
'-- Usage: StripAccent Sheet1.Range("A1:C20")
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
For i = 1 To Len(AccChars)
A = Mid(AccChars, i, 1)
B = Mid(RegChars, i, 1)
aRange.Replace What:=A, _
Replacement:=B, _
LookAt:=xlPart, _
MatchCase:=True
Next
End Sub
Last edited by anhn; Aug 16th, 2007 at 03:45 PM.
-
Aug 16th, 2007, 12:10 PM
#9
Thread Starter
Hyperactive Member
Re: Excel - Replace accented chars with regular chars
Last edited by xxarmoxx; Aug 16th, 2007 at 12:12 PM.
Reason: if he dies he dies
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|