Share via

word table loop serch, backward stuck with on result without change

li chen 0 Reputation points
2026-05-06T03:24:01.6566667+00:00

in a word VBA table loop serch, backward stuck with on result without change

Microsoft 365 and Office | Word | Other | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. Kai-H 16,355 Reputation points Microsoft External Staff Moderator
    2026-05-06T08:06:06.8566667+00:00

    Hi, li chen

    This usually happens because the search range is not being moved after a match is found, so Word keeps finding the same table cell again. It is more common in tables because Word has hidden cell/end-of-row markers.

    Here are some suggestions you can try:

    Use a Range, not Selection, and after each match reset the search range so the next backward search starts before the found text:

    Sub FindBackwardsInTables()
        Dim rng As Range
        Dim lastStart As Long
        Set rng = ActiveDocument.Range
        rng.Collapse wdCollapseEnd
        With rng.Find
            .ClearFormatting
            .Text = "your text here"
            .Forward = False
            .Wrap = wdFindStop
            Do While .Execute
                lastStart = rng.Start
                If rng.Information(wdWithInTable) Then
                    'do your table-related action here
                End If
                rng.End = lastStart
                rng.Start = ActiveDocument.Content.Start
                If rng.End <= rng.Start Then Exit Do
            Loop
        End With
    End Sub
    

    It is recommended that you also avoid wdFindContinue here, because it can wrap around and make the loop look endless. If it still sticks, add a check for the previous Start/End values and force the range back one character before continuing.

    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. 

    0 comments No comments

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.