DavG63
01-26-2016, 03:45 AM
Hi,
I have the following code in place, which is supposed to examine which rows in a table have no value and thereafter delete that row. In my document there are two identical tables, which appear at paragraphs 2 and 4.
Application.ScreenUpdating = FalseDim 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
For some reason each time this runs, I get an error "cannot access individual rows in this collection" once it reaches
For Each cel In Tbl.Rows(i).Cells
If I debug and remove Tbl.Rows and replace it with Tbl.Columns, I get an error, but then if I switch it back to Tbl.Rows it runs as it should.
Does anyone know why this would possibly be the case? It seems to defy logic as far as I'm concerned.
Thanks
Dav
I have the following code in place, which is supposed to examine which rows in a table have no value and thereafter delete that row. In my document there are two identical tables, which appear at paragraphs 2 and 4.
Application.ScreenUpdating = FalseDim 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
For some reason each time this runs, I get an error "cannot access individual rows in this collection" once it reaches
For Each cel In Tbl.Rows(i).Cells
If I debug and remove Tbl.Rows and replace it with Tbl.Columns, I get an error, but then if I switch it back to Tbl.Rows it runs as it should.
Does anyone know why this would possibly be the case? It seems to defy logic as far as I'm concerned.
Thanks
Dav