Posted By

hzarka on 10/11/11


Tagged

vba


Versions (?)

ConvertAccent


 / Published in: Visual Basic
 

  1. Function ConvertAccent(ByVal inputString As String) As String
  2. ' http://www.vbforums.com/archive/index.php/t-483965.html
  3.  
  4. Const AccChars As String = _
  5. "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
  6. Const RegChars As String = _
  7. "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
  8.  
  9. Dim i As Long, j As Long
  10. Dim tempString As String
  11. Dim currentCharacter As String
  12. Dim found As Boolean
  13. Dim foundPosition As Long
  14.  
  15. tempString = inputString
  16.  
  17. ' loop through the shorter string
  18. Select Case True
  19. Case Len(AccChars) <= Len(inputString)
  20. ' accent character list is shorter (or same)
  21. ' loop through accent character string
  22. For i = 1 To Len(AccChars)
  23.  
  24. ' get next accent character
  25. currentCharacter = Mid$(AccChars, i, 1)
  26.  
  27. ' replace with corresponding character in "regular" array
  28. If InStr(tempString, currentCharacter) > 0 Then
  29. tempString = Replace(tempString, currentCharacter, Mid$(RegChars, i, 1))
  30. End If
  31.  
  32. Next i
  33. Case Len(AccChars) > Len(inputString)
  34. ' input string is shorter
  35. ' loop through input string
  36. For i = 1 To Len(inputString)
  37.  
  38. ' grab current character from input string and
  39. ' determine if it is a special char
  40. currentCharacter = Mid$(inputString, i, 1)
  41. found = (InStr(AccChars, currentCharacter) > 0)
  42.  
  43. If found Then
  44.  
  45. ' find position of special character in special array
  46. foundPosition = InStr(AccChars, currentCharacter)
  47.  
  48. ' replace with corresponding character in "regular" array
  49. tempString = Replace(tempString, currentCharacter, _
  50. Mid$(RegChars, foundPosition, 1))
  51.  
  52. End If
  53. Next i
  54. End Select
  55.  
  56. ConvertAccent = tempString
  57. End Function

Report this snippet  

You need to login to post a comment.