In word if four numerals located add a zero at the beginning.

SteveD 625 Reputation points
2026-04-05T01:02:44.7833333+00:00

Hello from Steve

How do I do in VBA the below please.

In word if four numerals found add a zero at the beginning.

Developer technologies | Visual Basic for Applications

1 answer

Sort by: Most helpful
  1. AI answer

    2026-04-05T01:02:52.56+00:00

    Use a wildcard Find/Replace in VBA that looks for exactly four digits and replaces them with a leading zero plus the same four digits.

    Example macro:

    Sub AddZeroBeforeFourDigits()
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "<[0-9]{4}>"
            .Replacement.Text = "0\1"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
    End Sub
    

    Explanation:

    • .Text = "<[0-9]{4}>" finds any whole word that is exactly four numerals (0–9).
    • .Replacement.Text = "0\1" inserts a 0 in front of the found four-digit sequence.
    • wdReplaceAll updates all occurrences in the document.

    Run this macro on the document to prepend a zero to every standalone four-digit number.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    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.