Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2022, 03:17 AM
Catarina Catarina is offline Delete an MS Word table row based on next row text using VBA Windows 10 Delete an MS Word table row based on next row text using VBA Office 2016
Novice
Delete an MS Word table row based on next row text using VBA
 
Join Date: Jan 2022
Posts: 2
Catarina is on a distinguished road
Default Delete an MS Word table row based on next row text using VBA

Thank you in advance for your help on this.

I need to delete an MSWord table row based on the next row text.

My word table has headers which may or may not have sub-rows with details. I need to delete the header rows when there are no details below.

I can have, for example, this table, which is fine, no need to delete anything:

City
Lisbon
Porto
Madrid
London
Country
Portugal
Spain




But, if I have only this, then I need to delete the City header row (this is just an example, I have several header rows that need deleting when there are no contents below)

City
Country
Portugal
Spain

I tried adapting an earlier macro I had for deleting blank lines, but it is not working.

Code:
Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

' Specify which table you want to work on.
For Each oTable In ActiveDocument.Tables

' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
Set oNextRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

    StatusBar = "Row " & Counter
    DeleteRow = False

    For Each oCell In oRow.Rows(1).Cells
                       
        If oCell.Range.Text = "City" & Chr(13) & Chr(7) And oRow.Rows(1).Next.Cells(1).Range.Text = "Country" & Chr(13) & Chr(7) Then
            DeleteRow = True
            Exit For
        End If
    
    Next oCell

    If DeleteRow Then
        oRow.Rows(1).Delete
    Else
        Set oRow = oRow.Next(wdRow)
    End If

Next Counter

Next oTable
Application.ScreenUpdating = True
All help will be much appreciated
Reply With Quote
  #2  
Old 01-03-2022, 04:46 AM
gmayor's Avatar
gmayor gmayor is offline Delete an MS Word table row based on next row text using VBA Windows 10 Delete an MS Word table row based on next row text using VBA Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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
  #3  
Old 01-04-2022, 03:07 AM
Catarina Catarina is offline Delete an MS Word table row based on next row text using VBA Windows 10 Delete an MS Word table row based on next row text using VBA Office 2016
Novice
Delete an MS Word table row based on next row text using VBA
 
Join Date: Jan 2022
Posts: 2
Catarina is on a distinguished road
Default Worked!

Hi gmayor,

You are a life saveer, thanks a million, it worked

Happy New Year and many thanks again
Regards
Catarina
Reply With Quote
Reply

Tags
tables



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to hide/delete slides based on keyword in a separate text file based on AND|OR condition? rupd911 PowerPoint 0 02-22-2021 08:22 AM
Delete an MS Word table row based on next row text using VBA Macro to delete table rows based on the absence of a single specific keyword JellehFishh Word VBA 2 06-27-2019 08:23 AM
Word Macro to delete table row and table header NorthSuffolk Word VBA 6 10-11-2016 05:04 AM
Text inside text boxes create headings inside my table of contents!!! How do I delete the created he carstj Word 3 04-11-2016 12:46 PM
Delete an MS Word table row based on next row text using VBA Macro to color row of Word table based on found text kristib Word VBA 4 11-15-2015 02:42 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:00 AM.


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