View Single Post
 
Old 11-03-2021, 09:37 AM
JingleBelle JingleBelle is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2020
Posts: 26
JingleBelle is on a distinguished road
Default Delete First and Last Rows If....

With help from the experts at this forum, I crafted the following macro to search for a specific text string. If the text is found within a table, the row is deleted.

Would you help me further restrict this code to only look in the first row and the last row of all tables? I am certainly okay with running two macros if that is less complicated.

Thank you in advance for considering my request for help.

Code:
Sub DeleteRowIf()

Application.ScreenUpdating = False

  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
          MyRange.Rows(1).Delete
      End If
    Wend
  End With

Application.ScreenUpdating = True
Application.ScreenRefresh
MsgBox "Done!", vbInformation

End Sub
Reply With Quote