Consulting

Results 1 to 7 of 7

Thread: Delete blank rows in Word table

  1. #1

    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 
    
    
    Formatting tags added by mark007

    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 
    
    
    Formatting tags added by mark007
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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
    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
    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 
    
    
    Formatting tags added by mark007
    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
  •