Results 1 to 9 of 9

Thread: Excel - Replace accented chars with regular chars

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Excel - Replace accented chars with regular chars

    How can I use the replace function within the code? please post an example.

  4. #4

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Excel - Replace accented chars with regular chars

    Im on Excel 2004 for Macs. It should work the same as 2000 vba wise.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel - Replace accented chars with regular chars

    just made a dedicated function for what you want
    vb Code:
    1. Function repaccent(inputstr As String)
    2. mystr = ""     'put here the list of accented characters
    3. myrepstr = ""    'put here the list of characters to replace them with in the same order (position is critical)
    4. ' both strings must be the same length
    5. For i = 1 To Len(inputstr)
    6.     pos = InStr(mystr, Mid(inputstr, i, 1))  'check if each character is in the accented list
    7.     If Not pos = 0 Then inputstr = Left(inputstr, i - 1) & Mid(myrepstr, pos, 1) & Mid(inputstr, i + 1)
    8.     ' if it is replace it in the string with the equivalent character from the second string
    9. Next
    10. repaccent = inputstr
    11. End Function

    call it like
    vb Code:
    1. 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

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Thumbs up Re: Excel - Replace accented chars with regular chars

    Here you are. This function will work much better:

    VB Code:
    1. Option Explicit
    2.  
    3. '-- Add more chars to these 2 string as you want
    4. '-- You may have problem with unicode chars that has code > 255
    5. '-- such as some Vietnamese characters that are outside of ASCII code (0-255)
    6. Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
    7. Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
    8.  
    9. Sub StripAccent(aRange As Range)
    10. '-- Usage: StripAccent Sheet1.Range("A1:C20")
    11.    Dim A As String * 1
    12.    Dim B As String * 1
    13.    Dim i As Integer
    14.    
    15.    For i = 1 To Len(AccChars)
    16.       A = Mid(AccChars, i, 1)
    17.       B = Mid(RegChars, i, 1)
    18.       aRange.Replace What:=A, _
    19.                      Replacement:=B, _
    20.                      LookAt:=xlPart, _
    21.                      MatchCase:=True
    22.    Next
    23.  
    24. End Sub
    Last edited by anhn; Aug 16th, 2007 at 03:45 PM.

  9. #9

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Excel - Replace accented chars with regular chars

    Thanks guys!
    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
  •  



Click Here to Expand Forum to Full Width