Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2018, 02:30 PM
fbucaram fbucaram is offline Delete all empty rows in all tables Windows 10 Delete all empty rows in all tables Office 2016
Novice
Delete all empty rows in all tables
 
Join Date: Jan 2018
Posts: 3
fbucaram is on a distinguished road
Smile Delete all empty rows in all tables

Hi,



I am using the following macro to delete all rows in all tables of a word document:

Code:
Option Explicit

Public Sub DeleteEmptyRows()

Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

' Specify which table you want to work on.
For Each oTable In ActiveDocument.Tables
' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

    StatusBar = "Row " & Counter
    TextInRow = False

    For Each oCell In oRow.Rows(1).Cells
        If Len(oCell.Range.Text) > 2 Then
            'end of cell marker is actually 2 characters
            TextInRow = True
            Exit For
        End If
    Next oCell

    If TextInRow Then
        Set oRow = oRow.Next(wdRow)
    Else
        oRow.Rows(1).Delete
    End If

Next Counter
Next oTable
Application.ScreenUpdating = True

End Sub
It is working, however, all the columns that have empty rows end up opening very wide (see attached pic).

Can somebody please give me a hand?
Attached Images
File Type: jpg a.jpg (194.5 KB, 30 views)

Last edited by fbucaram; 01-03-2018 at 11:38 AM. Reason: add pic
Reply With Quote
  #2  
Old 01-02-2018, 10:24 PM
gmayor's Avatar
gmayor gmayor is offline Delete all empty rows in all tables Windows 10 Delete all empty rows in all tables Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following shouldn't change the format of the table, but if it does, post a document with the errant table. The macro will not work with vertically merged cells

Code:
Sub DeleteEmptyRows()
'Graham Mayor - http://www.gmayor.com - Last updated - 03 Jan 2018
Dim oTable As Table
Dim iRow As Integer
Dim iCol As Integer
    Set oTable = Selection.Tables(1)
    If Selection.Information(wdWithInTable) Then
        For iRow = oTable.Rows.Count To 1 Step -1
            iCol = oTable.Rows(iRow).Cells.Count
            If Len(oTable.Rows(iRow).Range) = (iCol * 2) + 2 Then
                oTable.Rows(iRow).Delete
            End If
        Next iRow
    Else
        MsgBox "Selection is not in a table"
    End If
lbl_Exit:
    Set oTable = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-02-2018, 10:35 PM
macropod's Avatar
macropod macropod is offline Delete all empty rows in all tables Windows 7 64bit Delete all empty rows in all tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: http://www.vbaexpress.com/forum/show...-in-all-tables
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

Neither Graham nor I enjoy re-inventing the wheel...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-03-2018, 11:35 AM
fbucaram fbucaram is offline Delete all empty rows in all tables Windows 10 Delete all empty rows in all tables Office 2016
Novice
Delete all empty rows in all tables
 
Join Date: Jan 2018
Posts: 3
fbucaram is on a distinguished road
Default Solved

Thank you and sorry for that. I am the new guy.


Quote:
Originally Posted by macropod View Post
Cross-posted at: http://www.vbaexpress.com/forum/show...-in-all-tables
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

Neither Graham nor I enjoy re-inventing the wheel...
Reply With Quote
  #5  
Old 01-03-2018, 03:35 PM
macropod's Avatar
macropod macropod is offline Delete all empty rows in all tables Windows 7 64bit Delete all empty rows in all tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Also cross-posted at: https://stackoverflow.com/questions/...rom-all-tables

Kindly provide the cross-post links between *all* of the forums you've done this on.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 01-05-2018, 02:34 PM
fbucaram fbucaram is offline Delete all empty rows in all tables Windows 10 Delete all empty rows in all tables Office 2016
Novice
Delete all empty rows in all tables
 
Join Date: Jan 2018
Posts: 3
fbucaram is on a distinguished road
Unhappy Cross post

There are no more cross posts that I know off.

Initially I thought forums were not related to each other and that replies were not so frequent. So I posted in many forums and lost track of them. Thank you for your patience.
Reply With Quote
  #7  
Old 01-05-2018, 03:04 PM
macropod's Avatar
macropod macropod is offline Delete all empty rows in all tables Windows 7 64bit Delete all empty rows in all tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by fbucaram View Post
Initially I thought forums were not related to each other
They aren't related, but that's not the issue. What your cross-posting has resulted in is three different people trying to help you on three different forums without the benefit of knowing what help you'd received elsewhere, resulting in a duplication of effort on two of them. The cross-posting rule/etiquette is about avoiding such duplication of effort and having people working with information about the issue on one forum that isn't available on the others (e.g. your mailmerge context).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete all empty rows in all tables Delete Empty Table Rows cltay87 Word VBA 4 02-27-2017 04:23 AM
Delete all empty rows in all tables Macro to delete all empty rows from all tables braddgood Word VBA 15 10-02-2015 01:54 PM
Delete all empty rows in all tables Delete blank rows between the two rows that contain data beginner Excel Programming 5 12-26-2014 12:29 AM
Delete all empty rows in all tables Delete All empty Rows - Print - Undo all Rows deleted Bathroth Word VBA 1 10-01-2014 01:40 PM
Delete all empty rows in all tables Macro to delete rows with all empty cells ubns Excel Programming 2 08-14-2012 02:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft