Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-31-2023, 02:41 AM
RobiNew RobiNew is offline Replace with zero any digit string of any length containing the letter O Windows 10 Replace with zero any digit string of any length containing the letter O Office 2016
Competent Performer
Replace with zero any digit string of any length containing the letter O
 
Join Date: Sep 2023
Posts: 208
RobiNew is on a distinguished road
Default 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

Reply With Quote
  #2  
Old 10-31-2023, 06:09 AM
gmaxey gmaxey is offline Replace with zero any digit string of any length containing the letter O Windows 10 Replace with zero any digit string of any length containing the letter O Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-31-2023, 06:52 AM
vivka vivka is offline Replace with zero any digit string of any length containing the letter O Windows 7 64bit Replace with zero any digit string of any length containing the letter O Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 10-31-2023, 07:14 AM
gmaxey gmaxey is offline Replace with zero any digit string of any length containing the letter O Windows 10 Replace with zero any digit string of any length containing the letter O Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

vivka,

Very nice.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 10-31-2023, 09:47 AM
vivka vivka is offline Replace with zero any digit string of any length containing the letter O Windows 7 64bit Replace with zero any digit string of any length containing the letter O Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

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)!
Reply With Quote
  #6  
Old 10-31-2023, 03:09 PM
RobiNew RobiNew is offline Replace with zero any digit string of any length containing the letter O Windows 10 Replace with zero any digit string of any length containing the letter O Office 2016
Competent Performer
Replace with zero any digit string of any length containing the letter O
 
Join Date: Sep 2023
Posts: 208
RobiNew is on a distinguished road
Default

Thanks Gmaxey! Thanks Vivka! They both work very nicely -- Gmaxey with {1;} rather than {1,}.
Reply With Quote
Reply



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
Replace with zero any digit string of any length containing the letter O Sort by String Length nmkhan3010 Word VBA 1 11-01-2021 01:50 PM
Replace with zero any digit string of any length containing the letter O Remove everything after the last instance of a digit/letter combo 14spar15 Excel 5 05-14-2019 05:29 AM
Replace with zero any digit string of any length containing the letter O Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Replace with zero any digit string of any length containing the letter O 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft