PDA

View Full Version : [SOLVED:] Macro: Delete single cell in word table row if contents are blank



DavG63
02-02-2016, 01:52 AM
Hi all

I have the following code which works for me in removing blank rows from a table on the Y-axis of the row is blank.

On another document I have, there are four table cells, spaced horizontally on the same row. What I'm looking to do is adapt the below check for an individual cell within that row as being blank, and if it finds one, to delete that cell and to shift any of the cells to the right of it which do have a value, to the left - in other words, potentially delete one of the middle boxes and then have it all automatically bunch back up.


Application.ScreenUpdating = FalseDim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
With ActiveDocument.Sections(2).Range
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

Is there any easy way to do this that anyone is aware of?

Thanks

Dav

gmayor
02-02-2016, 02:19 AM
Word is not Excel and handles tables differently. You cannot delete a cell in a Word table in the manner you describe. You could merge with the adjacent cell, but then addressing the table with VBA becomes a whole lot messier and is better avoided. If you want Excel functionality you would be better inserting an Excel object

DavG63
02-02-2016, 03:13 AM
Thanks Graham

I had suspected as much, but I'm loath to use an excel object in the document as it's just inviting yet more potential for something to go wrong (at least if my past attempts are anything to go by!).

I managed to resolve the issue somewhat, I couldn't delete the column, but I could define various strings and then depending on whether a particular combobox reports as null, shift the values along one. It means I potentially have an extra empty column or two at the end but it's no hardship in retrospect.

Will mark this as solved.

Thanks

Dav