Consulting

Results 1 to 6 of 6

Thread: Tables - Deleting blank rows

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location

    Tables - Deleting blank rows

    Hi,

    I have got a Word document with several tables, and in between each table is text.


    I need to delete the entire row only in tables that are blank or have no text.

    Any help would be very much appreciated?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    hubs

    You have code (from another post) that loops through document tables. This is not a free code writing service. Sometimes you will be handed the fish, but the idea is that you learn to catch your own fish.

    What have you tried?
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Hint 1. When looping through things (rows) where you may delete 1 or more of those things and affect the count, start at the end and work back to 1:

    For lngRow = oTbl.Rows.Count To 1 Step -1

    Hint 2. An empty row will have a text length equal to: (1 + number of cells) * 2.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location
    Quote Originally Posted by gmaxey View Post
    Hint 1. When looping through things (rows) where you may delete 1 or more of those things and affect the count, start at the end and work back to 1:

    For lngRow = oTbl.Rows.Count To 1 Step -1

    Hint 2. An empty row will have a text length equal to: (1 + number of cells) * 2.
    you are killing me haha

  5. #5
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location
    Quote Originally Posted by hubs View Post
    you are killing me haha
    The following code does the trick (code is plagiarised) but the method seems less efficient than yours

    Sub DeleteEmptyTableRows()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 End Sub

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    There you go. Something like this was what I had in mind:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table, cel As Cell, lngIndex As Long, n As Long, fEmpty As Boolean
      With ActiveDocument
        For Each oTbl In .Tables
          For lngIndex = oTbl.Rows.Count To 1 Step -1
            If Len(oTbl.Rows(lngIndex).Range.Text) = (oTbl.Rows(lngIndex).Cells.Count * 2) + 2 Then _
                                                     oTbl.Rows(lngIndex).Delete
          Next lngIndex
        Next oTbl
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    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
  •