PDA

View Full Version : [SOLVED:] Deleting rows based on adjacent blank cells in table



enjam
12-08-2016, 12:14 AM
Hello everyone,

I've just begun teaching myself VBA, and was wondering whether it would be possible to develop a macro that would delete rows in a table based on two adjacent cells?

The specific problem I'm trying to solve is in the attached document.

There are three columns comprising the table. I would like to develop a way of deleting an entire row if the center and rightmost columns of the row are blank. In this case, this would be the rows containing the words 'Trip Run No.','Bypass ID','Bondi Junction', and 'North Sydney Junction'.

Any assistance would be greatly appreciated.

gmaxey
12-08-2016, 12:30 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim lngIndex As Long
Set oTbl = Selection.Tables(1)
For lngIndex = oTbl.Rows.Count To 1 Step -1
'An empty cell contains a end of cell marker which has a length = 2. So,
If Len(oTbl.Cell(lngIndex, 2).Range) = 2 And Len(oTbl.Cell(lngIndex, 3).Range) = 2 Then
oTbl.Rows(lngIndex).Delete
End If
Next
lbl_Exit:
Exit Sub
End Sub

enjam
12-08-2016, 03:34 PM
That's fantastic! Thanks so much Greg, that was just what I was after.

Could you please tell me how I could modify the script so that it's applied to several tables in a document (rather than just the first one)?

gmaxey
12-08-2016, 04:28 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim lngIndex As Long
For Each oTbl in ActiveDocument.Tables
For lngIndex = oTbl.Rows.Count To 1 Step -1
'An empty cell contains a end of cell marker which has a length = 2. So,
If Len(oTbl.Cell(lngIndex, 2).Range) = 2 And Len(oTbl.Cell(lngIndex, 3).Range) = 2 Then
oTbl.Rows(lngIndex).Delete
End If
Next lngIndex
Next oTbl
lbl_Exit:
Exit Sub
End Sub