Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2021, 09:37 AM
JingleBelle JingleBelle is offline Delete First and Last Rows If.... Windows 10 Delete First and Last Rows If.... Office 2016
Novice
Delete First and Last Rows If....
 
Join Date: Nov 2020
Posts: 18
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
  #2  
Old 11-03-2021, 04:01 PM
Guessed's Avatar
Guessed Guessed is offline Delete First and Last Rows If.... Windows 10 Delete First and Last Rows If.... Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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
  #3  
Old 11-04-2021, 07:56 AM
JingleBelle JingleBelle is offline Delete First and Last Rows If.... Windows 10 Delete First and Last Rows If.... Office 2016
Novice
Delete First and Last Rows If....
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default

Hey, Andrew.


Thank you for adjusting the code for me. There is NO way I would have come up with that on my own.


It works like charm, and I appreciate the tip about the search term being in the first and second row. Of course, that had not occurred to me. I don't think that will be an issue.


The macro works as I wanted; however, it did fail when it encountered a table with a vertically merged cell. I had not considered that obstacle either. I think, though, in my notes somewhere or in some code I picked up on this forum, there is a way to avoid or skip over those tables. With any luck, I will be able to cobble the two together and get it to work. Having said that, I may be back.


Thank you, again, for your willingness to help me. I appreciate it very much.
Reply With Quote
Reply

Tags
delete table rows, first row last row, specified text string



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete First and Last Rows If.... Delete blank table rows in merged document and split document in docx and pdf based on excel rows Alex1s85 Word VBA 5 05-22-2021 12:05 PM
Delete First and Last Rows If.... Delete blank rows between the two rows that contain data beginner Excel Programming 5 12-26-2014 12:29 AM
Delete First and Last Rows If.... Delete All empty Rows - Print - Undo all Rows deleted Bathroth Word VBA 1 10-01-2014 01:40 PM
Find and Delete Rows damaniam Word VBA 1 03-11-2014 06:54 AM
Delete First and Last Rows If.... Delete all rows but the last. elky1967 Word VBA 14 09-21-2012 05:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft