Consulting

Results 1 to 9 of 9

Thread: Delete blank rows in Word table

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location

    Delete blank rows in Word table

    Hi

    I'm hoping someone will be able to give me a quick answer for this as I don't think the problem is that complicated - I just can't see the solution.

    I have several iterations of the same table in a Word document (repeated on several different letters).


    Each cell in the first column contains a single bullet point - that's it. Nothing else.
    Each cell in the second column contains information by way of DocVariables which may or may not display a value.

    I have code from a previous document that deletes out the rows in a table if they are blank.

    Application.ScreenUpdating = False
    Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
    With ActiveDocument
    For Each Tbl In .Tables
    n = Tbl.Rows.Count
    For i = n To 1 Step -1
    fEmpty = True
    For Each cel In Tbl.Rows(i).Cells
    If Len(cel.Range.Text) > 2 Then
    fEmpty = False
    Exit For
    End If
    Next cel
    If fEmpty = True Then Tbl.Rows(i).Delete
    Next i
    Next Tbl
    End With
    Set cel = Nothing: Set Tbl = Nothing
    Application.ScreenUpdating = True

    It isn't working this time however and I suspect it's because of the bulletpoint.

    Is there a way to tweak my code to ignore the initial column and delete the row if the cell in the second column is blank instead?

    Any help would be great.

    Thanks

    Dav

  2. #2
    How about

    Sub DeleteEmptyRows()
    Dim Tbl As Table, cel As Cell
    Dim i As Long, j As Long, n As Long, fEmpty As Boolean
        Application.ScreenUpdating = False
        With ActiveDocument
            For Each Tbl In .Tables
                n = Tbl.Rows.Count
                For i = n To 1 Step -1
                    fEmpty = True
                    For j = 2 To Tbl.Rows(i).Cells.Count
                        Set cel = Tbl.Rows(i).Cells(j)
                        If Len(cel.Range.Text) > 2 Then
                            fEmpty = False
                            Exit For
                        End If
                    Next j
                    If fEmpty = True Then Tbl.Rows(i).Delete
                Next i
            Next Tbl
        End With
        Set cel = Nothing: Set Tbl = Nothing
        Application.ScreenUpdating = True
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location
    Hi Graham

    Thanks for your suggestion. I've run this through and I'm afraid it doesn't appear to delete out the rows, I'm still left with either one or two rows with a bulletpoint and no information beside it.

    Any other ideas?

    Thanks

    Dav

  4. #4
    It should work, and does here if the remaining cells in the row are all empty - a space in a cell will cause it to ignore the row. Click the button on the Home tab to display the formatting or if you cannot find the cause post a copy of the table that doesn't work as a document attachment.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,797
    Location
    Try modifying this line:
    If Len(cel.Range.Text) > 2 Then
    to:
    If Len(Trim(cel.Range.Text)) > 2 Then
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    That should work for a space, but it won't for (say) a non-breaking space or a tab character. Without knowing what is in the cell, it is difficult to come up with a solution.
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,797
    Location
    Graham,

    True but just a bit bored and looking for something to do ;-).

    Perhaps:

    Sub DeleteEmptyRows()
        Dim Tbl As Table, cel As Cell
        Dim i As Long, j As Long, n As Long, fEmpty As Boolean
        Application.ScreenUpdating = False
        With ActiveDocument
            For Each Tbl In .Tables
                n = Tbl.Rows.Count
                For i = n To 1 Step -1
                    fEmpty = True
                    For j = 2 To Tbl.Rows(i).Cells.Count
                        Set cel = Tbl.Rows(i).Cells(j)
                        If Not fcnJustWhiteSpace(cel) Then
                           fEmpty = False
                           Exit For
                        End If
                    Next j
                    If fEmpty = True Then Tbl.Rows(i).Delete
                Next i
            Next Tbl
        End With
        Set cel = Nothing: Set Tbl = Nothing
        Application.ScreenUpdating = True
    End Sub
    Private Function fcnJustWhiteSpace(cel) As Boolean
    Dim oChr
      fcnJustWhiteSpace = True
      For Each oChr In cel.Range.Characters
        Select Case Asc(oChr)
          Case 9, 11, 13, 32, 160
          Case Else: fcnJustWhiteSpace = False: Exit For
        End Select
      Next
    End Function
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location
    Hi Greg/Graham

    Thanks very much for the suggestion - changing the code to
    If Len(Trim(cel.Range.Text)) > 2 Then
    worked a charm.

    I'll mark this as solved and thanks again.

    Dav

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,797
    Location
    Good. Worked because some of the cells contained one or more spaces. If you try the other version it should take care of that plus any other "white space" i.e, tabs, linebreaks, etc.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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