View Single Post
 
Old 01-03-2022, 04:46 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you are going to delete rows in a loop then you need to reverse the loop or the count goes haywire i.e.
Code:
For Counter = NumRows to 1 Step -1
However there is an assortment of other issues in your code. Try the following instead.

Code:
Sub Macro1()
Dim oTable As Table
Dim oRng As Range, oRng2 As Range
    For Each oTable In ActiveDocument.Tables
        Application.ScreenUpdating = False
        Set oRng = oTable.Range
        With oRng.Find
            .Text = "City"
            Do While .Execute
                If oRng.InRange(oTable.Range) Then
                    If Not oRng.Rows(1).Range.Start = oTable.Rows(1).Range.Start Then
                        Set oRng2 = oRng.Rows(1).Previous.Range.Rows(1).Cells(1).Range
                        If InStr(1, oRng2, "Country") > 0 Then oRng.Rows(1).Delete
                    End If
                End If
            Loop
        End With
        Set oRng = oTable.Range
        With oRng.Find
            .Text = "Country"
            Do While .Execute
                If oRng.InRange(oTable.Range) Then
                    If Not oRng.Rows(1).Range.Start = oTable.Rows(1).Range.Start Then
                        Set oRng2 = oRng.Rows(1).Previous.Range.Rows(1).Cells(1).Range
                        If InStr(1, oRng2, "City") > 0 Or InStr(1, oRng2, "Country") > 0 Then oRng2.Rows(1).Delete
                    End If
                End If
            Loop
        End With
    Next oTable
    Application.ScreenUpdating = True
    Set oTable = Nothing
    Set oRng = Nothing
    Set oRng2 = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote