Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Apr 2005
    Location
    Winston-Salem, North Carolina, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting blank rows in a table (Word 2000)

    I'm sure this has come up before, but I could not find reference to it. I need to search the current document and delete every row in every table if the entire row is blank. Am I headed in the right direction?

    If ActiveDocument.Tables.Count >= 1 Then
    Set oDoc1 = ActiveDocument
    For Each oTable In oDoc1.Tables
    For Each Row In oTable
    '
    '
    Next
    End If

    Thanks in advance.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Deleting blank rows in a table (Word 2000)

    If you want to delete rows, you must loop backwards in the inner loop, otherwise you'll be pulling the rug from under the code:

    For lngRow = oTable.Rows.Count To 1 Step -1
    If ... Then
    oTable.Rows(lngRow).Delete
    End If
    Next lngRow

  3. #3
    New Lounger
    Join Date
    Apr 2005
    Location
    Winston-Salem, North Carolina, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting blank rows in a table (Word 2000)

    Is there a way to tell if an entire row is blank or do I have to select each cell of the table and check them individually?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Deleting blank rows in a table (Word 2000)

    You'll have to check each cell. Please note that an empty cell contains two characters: a paragraph mark Chr(13) and an end-of-cell mark Chr(7).

  5. #5
    New Lounger
    Join Date
    Apr 2005
    Location
    Winston-Salem, North Carolina, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting blank rows in a table (Word 2000)

    Thanks for your help. At least now I have something that will work with all tables up to 7 columns and it could be increased. I'm sure there is a cleaner way, but this will work.

    Sub Delete_blank_rows()
    Dim odoc1 As Document
    Dim lngRow As Integer
    Dim lngcol As Integer
    Dim oTable As Table
    Dim itsblank1 As String
    Dim itsblank2 As String
    Dim itsblank3 As String
    Dim itsblank4 As String
    Dim itsblank5 As String
    Dim itsblank6 As String
    Dim itsblank7 As String

    MacroEntry
    itsblank1 = Chr(13) & Chr(7)
    itsblank2 = Chr(13) & Chr(7) & Chr(13) & Chr(7)
    itsblank3 = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7)
    itsblank4 = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7)
    itsblank5 = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7)
    itsblank6 = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7)
    itsblank7 = Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7) & Chr(13) & Chr(7)
    Selection.HomeKey Unit:=wdStory
    If ActiveDocument.Tables.Count >= 1 Then
    Set odoc1 = ActiveDocument
    For Each oTable In odoc1.Tables
    For lngRow = oTable.Rows.Count To 1 Step -1
    lngcol = oTable.Columns.Count
    If lngcol > 7 Then
    MsgBox "Can't delete blank rows from this table"
    Exit Sub
    End If
    oTable.Rows(lngRow).Select
    Selection.Collapse
    Selection.MoveRight Unit:=wdCharacter, Count:=lngcol, Extend:=wdExtend
    'MsgBox Selection.Text
    If Selection.Text = itsblank1 Or Selection.Text = itsblank2 Or Selection.Text = itsblank3 Or Selection.Text = itsblank4 Or Selection.Text = itsblank5 Or Selection.Text = itsblank6 Or Selection.Text = itsblank7 Then
    oTable.Rows(lngRow).Delete
    Else
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
    End If
    Selection.Collapse
    Next lngRow

    Next oTable
    End If
    MacroExit
    End Sub
    Sub MacroEntry()
    SBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = False
    End Sub

    Sub MacroExit()
    Application.StatusBar = False
    Application.DisplayStatusBar = SBar
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Deleting blank rows in a table (Word 2000)

    Another technique, which is not backwards compatible to Word 97, is to use the Replace function to clear out the end-of-cell markers and the end-of-row marker from the row's "text" and see whether there is anything left. Anything could include a space. However, this technique would lose track of stuff that isn't "text," which might include pictures, so it isn't quite as complete as one would like...

    <code>Function RowIsBlank(r As Word.Row) As Boolean
    If Len(Replace(r.Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    RowIsBlank = True 'row contain no text
    Else
    RowIsBlank = False 'row contains some text
    End If
    End Function

    Sub TEST_RowIsBlank()
    Dim rw As Word.Row
    With ActiveDocument.Tables(1)
    For Each rw In .Rows
    Debug.Print RowIsBlank(rw)
    Next
    End With
    End Sub</code>


    While this was set up as a function, you certainly could integrate the If test into your main procedure.

  7. #7
    New Lounger
    Join Date
    Apr 2005
    Location
    Winston-Salem, North Carolina, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting blank rows in a table (Word 2000)

    This works great for deleting blank rows from table 1, but what if I want to do all the tables in the document?

    Sub blank_rows()
    Dim odoc1 As Document
    Dim oTable As Table
    Dim rw As Word.Row

    MacroEntry
    Selection.HomeKey Unit:=wdStory
    If ActiveDocument.Tables.Count >= 1 Then
    Set odoc1 = ActiveDocument
    With odoc1.Tables
    With ActiveDocument.Tables(1)
    On Error Resume Next

    For Each rw In .Rows
    If RowIsBlank(rw) = True Then
    rw.Delete
    End If
    Next rw
    End With
    End With
    End If
    MacroExit
    Selection.HomeKey Unit:=wdStory
    MsgBox "Finished deleting blank rows"
    End Sub
    ===========================
    Sub MacroEntry()
    SBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = False
    End Sub
    ====================
    Sub MacroExit()
    Application.StatusBar = False
    Application.DisplayStatusBar = SBar
    Application.ScreenUpdating = True

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Deleting blank rows in a table (Word 2000)

    <P ID="edit" class=small>(Edited by jscher2000 on 23-Jun-05 11:07. Added the complete procedure at the end.)</P>Sorry, the TEST sub I posted is not really compatible with your original procedure. Here's how you might change that:

    <code>For Each oTable In odoc1.Tables
    For lngRow = oTable.Rows.Count To 1 Step -1
    If RowIsBlank(oTable.Rows(lngRow)) Then
    oTable.Rows(lngRow).Delete
    End If
    Next lngRow
    Next oTable</code>


    or

    <code>For Each oTable In odoc1.Tables
    For lngRow = oTable.Rows.Count To 1 Step -1
    If Len(Replace(oTable.Rows(lngRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    oTable.Rows(lngRow).Delete
    End If
    Next lngRow
    Next oTable</code>


    As you recall, your original "counting backwards" loop was necessary to avoid the problem of "skipping" blank rows in the collection.

    Note: I didn't test this on an actual document. Hopefully I didn't make any fatal typos there. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Added: Since there are a few other things you can delete, here is a "final" version that incorporates the above:

    <code>Sub Delete_blank_rows()
    Dim odoc1 As Document
    Dim lngRow As Integer
    Dim oTable As Table
    MacroEntry
    If ActiveDocument.Tables.Count >= 1 Then
    Set odoc1 = ActiveDocument
    For Each oTable In odoc1.Tables
    For lngRow = oTable.Rows.Count To 1 Step -1
    If Len(Replace(oTable.Rows(lngRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    oTable.Rows(lngRow).Delete
    End If
    Next lngRow
    Next oTable
    End If
    MacroExit
    End Sub </code>

  9. #9
    New Lounger
    Join Date
    Apr 2005
    Location
    Winston-Salem, North Carolina, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting blank rows in a table (Word 2000)

    That is exactly what I wanted. I did have to add On Error Resume Next because if a table has vertically merged cells, an error occurs. Thanks for the help!!

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Deleting blank rows in a table (Word 2000)

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> A nifty function, which I've stored for future use. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •