macro for deleting lines in word 2007

Microsoft Office 2007 professional (aca...
September 7, 2010 at 17:28:48
Specs: Windows XP
hello there, i have some documents in word 2007 filled by sharepoint, and what I want to do is this: there is one table in the document filled with some data (some measurements) and i want the macro to delete all data in the entire line which one or more collums are zero... something like this:


and it will make this


thanks in advance!

See More: macro for deleting lines in word 2007

Report •

September 8, 2010 at 04:24:36

The way that Word handles cells in tables is not as straightforward as the way cells are handled in Excel when cells are merged.

Are any of the cells in any of the tables ever merged (including header /label rows)

Do the tables have the same number of columns, or is this variable.


Report •

September 8, 2010 at 07:19:18
yes, only the headers have cells merged, but I dont want to delete them anyway

also, if there is no way for it to work, I can un-merge them or make another type of graphics to appear like a cell

also, the table ALWAYS have the same size when generated by sharepoint... today we open every generated file and manually select and delete the lines we dont want

Report •

September 8, 2010 at 11:13:47

I will have a go at it tomorrow.

Do any tables have vertically merged cells.


Report •

Related Solutions

September 8, 2010 at 14:49:55

Here is a macro that will delete a row or rows in tables in a word document where every cell in that/those row(s) is zero.

The macro will work with tables with merged cells in header rows. It has not been tested with zero data in merged cells. It may not delete 'all zero' rows appropriately if the there are merged cells in those rows.

It will also not delete the first row in a table even if it contains all zero values. (This could be changed - but in one of the responses it was stated that header rows would not be deleted and I assumed (dangerous, I know) that all tables would have headers.

Here is the macro:

Option Explicit

Sub RemZeroLn()
Dim wdTable As Table
Dim wdCell As Cell
Dim intCells As Integer
Dim intThisRow As Integer
Dim intLastRowCell As Integer
Dim strCellText As String
Dim blnAllZeros As Boolean
Dim m, n As Integer

On Error GoTo ErrHnd

'loop through all tables in the document
For Each wdTable In ActiveDocument.Tables()
    'get number of cells in table
    intCells = wdTable.Range.Cells.Count
    'set 'This' row variable
    intThisRow = wdTable.Range.Cells(intCells).RowIndex
    'set last cell count
    intLastRowCell = intCells
    'set AllZeros flag to true
    blnAllZeros = True
    For m = intCells To 1 Step -1
        'test if we have moved into a different row
        If wdTable.Range.Cells(m).RowIndex <> intThisRow Then
            'test if need to delete row
            If blnAllZeros = True Then
                'all zeros - so delete row of cells
                For n = intLastRowCell To m + 1 Step -1
                Next n
            End If
            'in a new row - so reset flag, This row and Last cell
            intThisRow = wdTable.Range.Cells(m).RowIndex
            intLastRowCell = m
            blnAllZeros = True
        End If
        'test cell contents - need to remove table marker
        strCellText = Left(wdTable.Range.Cells(m).Range, _
                    Len(wdTable.Range.Cells(m).Range) - 2)
        If strCellText = "" Then
            'its empty, so its not a zero
            blnAllZeros = False
            ElseIf IsNumeric(strCellText) Then
                'its a number - so test if not zero
                If CDbl(strCellText) <> 0 Then
                    blnAllZeros = False
                End If
            'not empty and not a number so row is not all zeros
            blnAllZeros = False
        End If
    Next m
Next wdTable
Exit Sub

'error handler
MsgBox "Macro error - please check data"
End Sub

As changes made by macros in Word, cannot always be undone the macro should be tested on copies of documents. Ensure that the macro works 'as expected'. The macro has only been tested on very limited sample data and has not been tested in your environment. Always make a backup of documents before running the macro on them.

I included a short warning message if the macro encounters an error. This is just to provide a warning, and it does not provide any debugging information.


Report •

Ask Question