Share via

Word if not found then

SteveD 145 Reputation points
2026-03-25T20:11:02.0266667+00:00

Hello from Steve

Objective if not found then move to next

Sub Moving_Numerals()

Application.ScreenUpdating = False

Do

Selection.Find.ClearFormatting

Selection.Find.Font.Size = 12

With Selection.Find

    .Text = "|"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = False

End With

Selection.Find.Execute

If Selection.Find.found Then

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.HomeKey

With Selection.Find

    .Text = "[0-9]{1,}*[A-Z]{1,}"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = True

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.Find.Font.Size = 10.5

With Selection.Find

    .Forward = False

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = True

    .Execute

End With

Selection.MoveDown Unit:=wdLine, Count:=3

Selection.HomeKey Unit:=wdLine

Selection.MoveRight Unit:=wdCharacter, Count:=3

Selection.Extend

With Selection.Find

    .Text = " [A-Z]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.MoveLeft Unit:=wdCharacter, Count:=2

Selection.Copy

Selection.MoveUp Unit:=wdLine, Count:=1

Selection.HomeKey Unit:=wdLine

Selection.MoveUp Unit:=wdLine, Count:=2

    With Selection.Find

    .Text = "[0-9]{1,}*[A-Z]{1,}"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.Fields.Update

strText = Selection.Text

Selection.Collapse Direction:=wdCollapseStart

Selection.MoveDown Unit:=wdLine, Count:=4

With Selection.Find

    .Forward = True

    .Wrap = wdFindStop

    .MatchCase = True

    .MatchWholeWord = True

    .Text = strText

    .MatchWildcards = True

    .Execute

    Selection.MoveLeft Unit:=wdCharacter, Count:=2

    With Selection.Find

    .Text = "[A-Z]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindStop

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchAllWordForms = False

    .MatchSoundsLike = False

    .MatchWildcards = True

End With

Selection.Find.Execute

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.Paste

End With

Selection.TypeText Text:=" "

Selection.MoveDown Unit:=wdLine, Count:=1

Selection.HomeKey Unit:=wdLine

End If

Loop While Selection.Find.found

Application.ScreenUpdating = True

End Sub

Microsoft 365 and Office | Word | For home | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-25T20:11:13.7166667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use the return value of Selection.Find.Execute (or the Found property) to decide whether to run the rest of the logic, and exit the loop when nothing is found. The Execute method returns True if something is found and False if not.

    A minimal pattern based on the documented behavior is:

    Sub Moving_Numerals()
    
        Application.ScreenUpdating = False
    
        Do
            With Selection.Find
                .ClearFormatting
                .Text = "|"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchAllWordForms = False
                .MatchSoundsLike = False
                .MatchWildcards = False
            End With
    
            ' Execute returns True if found, False if not
            If Not Selection.Find.Execute Then
                Exit Do          ' nothing found: leave the loop
            End If
    
            ' at this point, "|" has been found – run the rest of the code
            ' ... your existing processing here ...
    
        Loop
    
        Application.ScreenUpdating = True
    
    End Sub
    

    Key points:

    • Find.Execute returns a Boolean (True if the find operation is successful).
    • Test that Boolean immediately and Exit Do (or Exit Sub) when it is False.
    • This avoids relying on Selection.Find.Found at the bottom of the loop and ensures the macro stops cleanly when the search text is not found.

    If additional Find operations inside the loop may also fail, guard each of them similarly:

    If Not Selection.Find.Execute Then
        ' not found – either skip this block or exit
        GoTo NextIteration   ' or Exit Do / Exit Sub
    End If
    
    ' code that assumes a successful find
    

    Then place a NextIteration: label just before Loop if using GoTo.


    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.