Deleting cells with VBA

Discussion in 'Word VBA Beginners' started by Morales, Apr 24, 2007.

  1. Morales

    Morales Guest

    Hi All, I have already posted this message once, but I can't see it.
    If it pops up later - apologies.
    Can any of you gurus tell me how to select and delete a row of cells
    using VBa if the row is empty. In most cases the tables will be one
    cell deep by two cells wide. Thanks in advance.
    Morales, Apr 24, 2007
    1. Advertisements

  2. Morales

    macropod Guest

    Hi Morales,

    The following code will delete all blank rows (except for those with vertically merged cells) in all tables in the document:

    Sub DelBlankRows()
    Dim Pwd As String
    Dim oRow As Integer
    Dim oTable As Table
    With ActiveDocument
    If .Tables.Count > 0 Then
    For Each oTable In .Tables
    For oRow = oTable.Rows.Count To 1 Step -1
    If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    On Error Resume Next 'skip vertically merged cells
    End If
    Next oRow
    Next oTable
    End If
    End With
    End Sub

    macropod, Apr 24, 2007
    1. Advertisements

  3. Morales

    macropod Guest

    You can delete 'Dim Pwd As String' - it was left over from another project.

    [MVP - Microsoft Word]

    macropod, Apr 24, 2007
  4. Morales

    Greg Maxey Guest

    Something like this:

    Sub ScratchMacro()
    Dim oTbl As Word.Table
    Dim bEmpty As Boolean
    Dim oRow As Word.Row
    Dim oCell As Word.Cell
    For Each oTbl In ActiveDocument.Range.Tables
    For Each oRow In oTbl.Rows
    bEmpty = True
    For Each oCell In oRow.Cells
    If Len(oCell.Range.Text) > 2 Then bEmpty = False
    Next oCell
    If bEmpty Then oRow.Delete
    Next oRow
    Next oTbl
    End Sub
    Greg Maxey, Apr 24, 2007
  5. Morales


    Sep 10, 2012
    Likes Received:
    In my word document I have tables in which cells are vertically merged. Could you please let me know how to delete empty rows of such tables?
    tajmadhu, Sep 10, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.