Share via

vba: Objective replace three zeros

SteveD 430 Reputation points
2026-04-19T22:01:41.55+00:00

Hello from Steve

Objective please replace with 3 zeros at the beginnjing when three digits are found

Sub AddZeroBeforeThreeDigits()

With Selection.Find

    .ClearFormatting

    .Replacement.ClearFormatting

    ' Find any 3-digit number as a group

    .Text = "([0-9]{3})"

    ' Replace with 0 + the captured group

    .Replacement.Text = "0\3"

    .Forward = True

    .Wrap = wdFindStop   ' stop when end reached

    .Format = False

    .MatchWildcards = True

End With

' Execute the find

If Selection.Find.Execute Then

    ' Replace only this one occurrence

    Selection.Find.Execute Replace:=wdReplaceOne

End If
```End Sub

Developer technologies | Visual Basic for Applications

2 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 6,785 Reputation points Microsoft External Staff Moderator
    2026-04-21T03:40:05.5533333+00:00

    Hi @SteveD .

    You’re close, but a couple Word-VBA wildcard details are off:

    • \3 refers to the 3rd capture group. You only have one group, so you want \1.
    • If your goal is to prefix any 3-digit number with three zeros (e.g., 123000123), use 000\1.
    • Also, your current code only replaces the first match. Use wdReplaceAll to replace all matches in the selection.
    
    Sub AddZeroBeforeThreeDigits()
    
        With Selection.Find
    
            .ClearFormatting
    
            .Replacement.ClearFormatting
    
            ' Find any 3-digit number
    
            .Text = "([0-9]{3})"
    
            ' Prefix the match with three zeros
    
            .Replacement.Text = "000\1"
    
            .Forward = True
    
            .Wrap = wdFindStop
    
            .Format = False
    
            .MatchWildcards = True
    
            ' Replace all occurrences
    
            .Execute Replace:=wdReplaceAll
    
        End With
    
    End Sub
    
    

    If you actually meant “replace the 3 digits with 000” (so 123 becomes 000), then just set:

    .Replacement.Text = "000".

    Here's the result with the macro text above:

    Before:

    User's image

    After

    User's image

    If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

    Thank you. 

    1 person found this answer helpful.

  2. Q&A Assist
    2026-04-19T22:01:49.4433333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use the wildcard backreference \1 (not \3) and let Word do the ReplaceAll in one Execute call.

    To add a single leading zero to every 3‑digit number:

    Sub AddZeroBeforeThreeDigits()
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
    
            ' Find any 3-digit number
            .Text = "([0-9]{3})"
    
            ' Add one leading zero before the 3-digit number
            .Replacement.Text = "0\1"
    
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = True
    
            ' Replace all occurrences
            .Execute Replace:=wdReplaceAll
        End With
    End Sub
    

    If the goal is to make every 3‑digit number into a 6‑digit number by adding three zeros in front (for example, 123000123), change the replacement text to:

    .Replacement.Text = "000\1"
    

    This uses the same wildcard pattern as in the documentation examples: define the search pattern in .Text, define the replacement in .Replacement.Text, then call .Execute with the appropriate Replace value (wdReplaceAll, wdReplaceOne, or wdReplaceNone).


    References:

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.