Share via

Word finding X at the beginning of numerals

SteveD 430 Reputation points
2026-04-20T04:03:02.13+00:00

Hello from Steve

Is it possible please using the below [0-9XL]{5} to also find X at the beginning.

For example X9937

Sub Test ()

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "[0-9XL]{5}"

    .Replacement.Text = ""

    .Forward = False

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute
```Sub End
Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author and recommended by moderator

  1. Kai-H 16,355 Reputation points Microsoft External Staff Moderator
    2026-04-20T12:43:39.0033333+00:00

    Hi, SteveD

    If you just want to allow X anywhere in the 5 characters, then [0-9XL]{5} already does that. In Word wildcards, [ ] means “any one of these characters” and {5} means “exactly five of them,” so X9937 is a valid match.

    If you want X specifically in the first position, then the better pattern is:

    .Text = "X[0-9]{4}"
    

    That will match X9937, X1234, and so on, but not values where the X appears later. If you want X or L only at the start, use:

    .Text = "[XL][0-9]{4}"
    

    One other small thing: your macro is searching backwards because .Forward = False, and with wdFindStop it stops at the document boundary, so where the cursor is placed can affect whether it finds the match.

    Thank you for your patience in reading, I hope this information has been helpful to you. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment."    

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Answer accepted by question author

  1. Q&A Assist
    2026-04-20T04:03:10.8766667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use a pattern that allows an optional leading X followed by four characters from the same set. For example:

    Sub Test()
    
        Selection.Find.ClearFormatting
    
        With Selection.Find
            .Text = "X[0-9XL]{4}"    ' X followed by 4 of 0-9, X, or L
            .Replacement.Text = ""
            .Forward = False
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
    
        Selection.Find.Execute
    
    End Sub
    

    This finds strings like X9937 (X + 4 characters from the set). If both X9937 and values without X (such as L9937) must be matched with the same pattern, use an alternation:

    .Text = "X[0-9XL]{4}|[0-9XL]{5}"
    

    This matches either 5 characters from [0-9XL] or an X followed by 4 characters from [0-9XL].


    References:

    1 person found this answer helpful.

0 additional answers

Sort by: Most 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.