• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1033
  • Last Modified:

VBA Word ¦ Search for contents in Table Cells and delete adjacent row.

Hello all,

How can I pinpoint the location of a Cell within a Table, and delete the row underneath it? I have vertically merged cells, and apparently the Rows collection fails.

The Cell contains a keyoword such as: "Application Domain " or "Subdomain " and I want to delete the row under the cells which contain any of these strings.

Should I look into a Macro that uses the Search Function, or somehow run through each cell of the Table?

What is a clever way of dealing with this?

Thank you very much,
1 Solution
Manipulating tables with merged cells is extremely difficult.

This might work, depending on the exact layout of the table.

Sub DeleteNextRow()
    Dim cl As Cell
    Dim cl1 As Cell
    Dim rngFind As Range
    Dim rngCells As Range
    Dim tbl As Table
    Dim v As Integer
    Dim v1 As Integer
    Dim cs As Long
    Set tbl = ActiveDocument.Tables(1)
    Set rngFind = tbl.Range
    'Dim h As Integer
    If rngFind.Find.Execute("Application Domain") Then
        For Each cl In tbl.Range.Cells
            If rngFind.InRange(cl.Range) Then
                Exit For
            End If
        Next cl
    v = cl.Range.Information(wdStartOfRangeRowNumber)
        Set cl = cl.Next
        Debug.Print cl.Range.Text
    Loop Until cl.Range.Information(wdStartOfRangeRowNumber) > v
    v1 = cl.Range.Information(wdStartOfRangeRowNumber)
    cs = cl.Range.Start
        Set cl = cl.Next
    Loop Until cl.Range.Information(wdStartOfRangeRowNumber) > v1
    Set cl = cl.Previous
    Set rngCells = ActiveDocument.Range(cs, cl.Range.End)
    rngCells.Cells.Delete wdDeleteCellsEntireRow
    End If
End Sub
vRiscAuthor Commented:
That is simply great, thank you.

One last question: How can I have rngFind.Find.Execute() search for either "Application Domain" OR "Subdomain". In my table, an Application Domain can have 1 or more Subdomains.

The order it finds the rows is important, as I want to modify this code to copy the Description row together with the current Domain row to a different file.

Thanks a bunch, this was lovely.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now