View Single Post
 
Old 11-03-2021, 04:01 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This shows you the extra test to work out if it is a first or last row.
Code:
Sub DeleteRowIf()
  Dim MyRange As Range
  Set MyRange = ActiveDocument.Range
  With MyRange.Find
    .ClearFormatting
    .Text = "TEXT TO FIND"
    .MatchCase = True
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    While .Execute
      If MyRange.Information(wdWithInTable) Then
        If MyRange.Rows.First.Range = MyRange.Tables(1).Rows.First.Range Or MyRange.Rows.First.Range = MyRange.Tables(1).Rows.Last.Range Then
          MyRange.Rows(1).Delete
        End If
      End If
    Wend
  End With
  MsgBox "Done!", vbInformation
End Sub
The issue is if your search term is in both first and second rows. After the first row is deleted, the second row becomes the NEW first row so it will also be deleted. If that is going to be a problem, you can move the range so it searches down from the subsequent row.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote