Excel Macro to Find & Replace Diacritic Characters

excel8442

New Member
Joined
Mar 18, 2016
Messages
24
I am trying the following but getting an error (on the row with --> ActiveDocument.Range.Find.Execute) in Excel 2011 for Mac. Any ideas?

Sub Diacritics_Replacement()


Dim strFind As String
Dim strReplace As String
Dim i As Long
strFind = "áéíóú"
strReplace = "aeiou"
For i = 1 To Len(strFind)
ActiveDocument.Range.Find.Execute FindText:=Mid$(strFind, i, 1), _
ReplaceWith:=Mid$(strReplace, i, 1), Replace:=wdReplaceAll
Next i


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Thanks Rick!

Here is my macro with the characters from the last message at the link you posted...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ReplaceDiacriticCharacters()
  Dim X As Long, Diacritic As Variant, Normal As Variant
  Diacritic = Split(Trim(Replace(StrConv("ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ", vbUnicode), Chr(0), " ")))
  Normal = Split(Trim(Replace(StrConv("AAAAAAaaaaaaEEEEeeeeIIIIiiiiNnOOOOOoooooUUUUuuuuYyy", vbUnicode), Chr(0), " ")))
  For X = 0 To UBound(Diacritic)
    ActiveSheet.UsedRange.Replace Diacritic(X), Normal(X), xlPart, MatchCase:=True
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I find that when I run this (working) macro I end up with some incorrect diacritic replacements (due to errors in my source data probably).

I'd like to:
- run the macro to do all replacements en masse
- "mark" the cells that contain any replacements in some way (colour the cells "red" perhaps?)
- manually "cycle through" the red cells and manually "fix" any problems

How do I colour the cells in the macro and then cycle through the red cells?
 
Upvote 0
I find that when I run this (working) macro I end up with some incorrect diacritic replacements (due to errors in my source data probably).
You can copy the two quoted strings from the assignment lines to Diacritic and Normal and paste them into the Immediate Window, one under the other... the diacritic characters and their replacement characters will line up, one under the other, and you can check if you made a mistake in setting those text strings up. If they line up correctly, then you must have missed accounting for some diacritic characters... locate them and add them, and their replacements, into the two text strings (just make sure the diacritic and its replacement line up one under the other.


I'd like to:
- run the macro to do all replacements en masse
- "mark" the cells that contain any replacements in some way (colour the cells "red" perhaps?)
- manually "cycle through" the red cells and manually "fix" any problems
I don't understand this... the only characters I can test are already accounted for in the two text strings... if you missed any diacritic characters, the code will not be able to detect them so those cells would remain uncolored.
 
Upvote 0
This isn't the issue. Nothing wrong with your macro at all or either list of chars.

I have peculiarities in the source file. MOST of the time your macro will do what I want. In (unpredictable) cases the resulting replacement will not be the char I actually want (entirely unpredictable). Because of this I need to manually check after-the-fact (after your macro has run). I'd prefer to do it AFTER your macro has run because your macro will be correct 90% of the time.

So my idea is:
-1- run (a modified version) of your macro to replace Diacritics AND WHICH COLORS THE CELLS "RED" IF ANY CHARS WERE REPLACED.
-2- I manually FIND based on cell fill color = RED and eyeball the cell contents and fix any bad chars manually.
-3- I then select all and recolor everything to "No Color"

Since then I've encountered a "complication" to point #2 above:

I am running Excel 2011 for Mac and (apparently unlike on Windows) there is no automatic way to FIND based on CELL COLOR -- is that correct?

Hope this makes sense. Perhaps you have a better procedure.
 
Last edited:
Upvote 0
Hope this makes sense. Perhaps you have a better procedure.

I have a couple of other possible "ideas" but I don't know if they can be done:

-1- Use FILTER rather than FIND. If I could filter to see just the cells (rows) that CONTAIN 1 or more chars from ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ then I could FIX my problem items simply before running your macro (the un-modified version as above). That would be a simple solution perhaps.

-2- Similarly, use FILTER rather than FIND to see just the RED Cells (Rows) that have been coloured RED by a modified version of your macro.
 
Upvote 0
-1- run (a modified version) of your macro to replace Diacritics AND WHICH COLORS THE CELLS "RED" IF ANY CHARS WERE REPLACED.
Run this macro BEFORE you run the one that does the replacing (otherwise there will be no diacritic characters to find)...
Code:
[table="width: 500"]
[tr]
	[td]Sub RedDiacritics()
  Dim X As Long, Diacritics() As String
  Diacritics = Split(StrConv("ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ", vbUnicode), Chr(0))
  Application.ReplaceFormat.Clear
  Application.ReplaceFormat.Interior.Color = vbRed
  Application.ScreenUpdating = False
  For X = 0 To UBound(Diacritics) - 1
    ActiveSheet.UsedRange.Replace Diacritics(X), "", xlPart, SearchFormat:=False, ReplaceFormat:=True
  Next
  Application.ScreenUpdating = True
  Application.ReplaceFormat.Clear
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Run this macro BEFORE you run the one that does the replacing (otherwise there will be no diacritic characters to find)...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub RedDiacritics()
  Dim X As Long, Diacritics() As String
  Diacritics = Split(StrConv("ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ", vbUnicode), Chr(0))
  Application.ReplaceFormat.Clear
  Application.ReplaceFormat.Interior.Color = vbRed
  Application.ScreenUpdating = False
  For X = 0 To UBound(Diacritics) - 1
    ActiveSheet.UsedRange.Replace Diacritics(X), "", xlPart, SearchFormat:=False, ReplaceFormat:=True
  Next
  Application.ScreenUpdating = True
  Application.ReplaceFormat.Clear
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Thanks for this code.

I'm getting a runtime error 438 at the line: Application.ReplaceFormat.Clear
(Object doesn't support this property or method).

If I delete that line I get the same error message on the next line...
 
Upvote 0
Thanks for this code.

I'm getting a runtime error 438 at the line: Application.ReplaceFormat.Clear
(Object doesn't support this property or method).

If I delete that line I get the same error message on the next line...
What version of Excel are you using?

Are you running Excel on a Mac?
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,784
Members
449,124
Latest member
shreyash11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top