#1
|
|||
|
|||
Replace with zero any digit string of any length containing the letter O
I am trying to work out a macro that replaces the letter O with zero ('0') in any position within a digit string. I cannot go any further into the tentative code here below. Thanks! Code:
Sub LetterToZero() 'Replace with zero any digit string of any length containing the letter O 'Test sequence: This sequence 145O or this 4O56 or this O4578 Set aRng = ActiveDocument.StoryRanges(wdMainTextStory) With aRng.Find .ClearFormatting .Text = "O[0-9]" ' .Text = "[0-9]O" ' find letter O inside digit string ' .Replacement.Text = "0" 'Replace only the letter O .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWildcards = True .Execute 'If .Found = True Then MsgBox "Found " & aRng.Text End With End Sub |
#2
|
|||
|
|||
Something better may come along, but this should work:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "[0-9Oo]{1,}" .MatchWildcards = True While .Execute oRng.Text = Replace(oRng.Text, "O", "0") Wend End With lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Hi! Greg's macro is very interesting (as usual). As a variant, you can try this simple but swift code:
Code:
Sub O_To_Zero() ''Replace letters "O" followed and/or preceded by a digit 'with zeros. Set aRng = ActiveDocument.StoryRanges(wdMainTextStory) With aRng.Find .ClearFormatting .Replacement.ClearFormatting .text = "O([0-9])" .Replacement.text = "0\1" .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll .text = "([0-9])O" .Replacement.text = "\10" .Execute Replace:=wdReplaceAll End With End Sub |
#4
|
|||
|
|||
vivka,
Very nice. |
#5
|
|||
|
|||
Thank you, Greg! Your praise is of great value for me, because you are one of my teachers here (of course it's a revelation for you)!
|
#6
|
|||
|
|||
Thanks Gmaxey! Thanks Vivka! They both work very nicely -- Gmaxey with {1;} rather than {1,}.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Replace apostrophe mark before a digit | RobiNew | Word VBA | 13 | 10-22-2023 05:37 PM |
Sort by String Length | nmkhan3010 | Word VBA | 1 | 11-01-2021 01:50 PM |
Remove everything after the last instance of a digit/letter combo | 14spar15 | Excel | 5 | 05-14-2019 05:29 AM |
Wildcard replace any string in context with a specified string | wardw | Word | 7 | 05-07-2018 09:13 AM |
A challenging digit by digit manipulation, rotate the digit in range of 0 to 9 | laucn | Excel Programming | 14 | 05-17-2015 12:12 PM |