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

Posted on 2007-07-26
Last Modified: 2008-01-09
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,
Question by:vRisc
    LVL 76

    Accepted 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

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now