Unable to delete blank cells in 2nd or subsequent tables

hornbyOO

New Member
Joined
Sep 13, 2012
Messages
6
Hi
I've wriiten a small word macro to loop through a series of tables and delete any blank cells, Now it works fine for the 1st table and correctly selects the second table but then deletes a cell from the 1st table, and now I'm a tad lost as to why this is, I wrongly assumed changing the 1 to 2 /3/4 in Selection.Tables(1) would tell the macro which table to use. If I comment out the delete cells the macro will correctly loop through any amount of tables. (msgbox is purely ro help me during run through).

Word Macro
Code:
Sub FindTables()

Dim iResponse As Integer
Dim tTable As Table
Dim oCell As Cell
Dim oRow As Row

'If any tables exist, loop through each table in collection.
    
    For Each tTable In ActiveDocument.Tables
        tTable.Select
        For Each oRow In Selection.Tables(1).Rows
            For Each oCell In oRow.Cells
                If oCell.Range.Text = Chr(13) & Chr(7) Then
                    MsgBox oCell.RowIndex & " " & oCell.ColumnIndex & " is empty."
                    ActiveDocument.Tables(1).Cell(oCell.RowIndex, oCell.ColumnIndex).Delete
                End If
            Next oCell
        Next oRow
    Next
    MsgBox prompt:="Search Complete.", Buttons:=vbInformation

End Sub
The word document has a number of 11 single column tables with currently 7 rows with the top cell being the header, once empty cells are deleted I want to format the text.

Table1
DETECTORS
SMOKE

Table 2
WALKWAYS
CLEAR

Table 3
SIGNAGE
INPLACE

Table 4
OXYGEN
AMBIENT

Table 5
FIRE DOORS
NO

Table 6
SOURCES OF IGNITION
CANDLES
FLY KILLER
KETTLE
WASHING MACHINE

Table 7
FIRE BLANKET
NONE

Table 8 to 11 similar with up to 7 entries
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Just giving a quick once over, you start with a document, get all tables and loop thru, when you come to delete you use activedocument.tables(1) surely this is at the top level again and not at the current table/current row, surely it needs to be at ttable level at least,that's why it works for the first one, and on the second time thru the loop for table 2 it deletes the corresponding cell from table 1
 
Upvote 0
Aah haa, makes sense and as you say explains why it works on 1st table. I'll give it a whirl.

Many Thanks, at least I may have some hair left.
 
Upvote 0
Try:
Code:
Sub CleanTables()
Dim Tbl As Table, oRow As Row
For Each Tbl In ActiveDocument.Tables
    For Each oRow In Tbl.Rows
        If Len(oRow.Range.Text) = (oRow.Cells.Count * 2 + 2) Then oRow.Delete
    Next oRow
Next Tbl
MsgBox prompt:="Search Complete.", Buttons:=vbInformation
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,090
Members
453,337
Latest member
fiaz ahmad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top