PDA

View Full Version : Delete data from selected multiple Word tables



atuck88
02-12-2022, 03:49 AM
I have the following code which deletes any row in columns 4-8 of a word table with a '-' symbol.


Sub Delete_Table_Rows_With_No_Data()
Dim objCell As Range
Dim nRowIndex As Integer, nRows As Integer, nColumns As Integer, nColumnIndex As Integer
Dim varCellEmpty As Boolean

Application.ScreenUpdating = False

If Selection.Information(wdWithInTable) = False Then
MsgBox ("Put cursor inside a table first!")
Exit Sub
Else
With Selection.Tables(1)
nRows = .Rows.Count
For nRowIndex = nRows To 1 Step -1
varCellEmpty = True
For nColumns = 5 To .Columns.Count
Set objCell = .Rows(nRowIndex).Cells(nColumns).Range
objCell.End = objCell.End - 1
If Len(objCell) > 0 And Not objCell.Text = "-" Then
varCellEmpty = False
Exit For
End If
Next nColumns
If varCellEmpty = True Then .Rows(nRowIndex).Delete
Next nRowIndex
End With
End If
Set objCell = Nothing
Application.ScreenUpdating = True
End Sub




At present I have to go through and run the macro for each table.

I would like to program the macro so it runs for selected tables in the document but I'm unsure of the code to achieve this.

For instance, the first table in the document does NOT require the macro but tables X-XX which contain data DO require it.

Im a bit lost. Any Ideas?

Thanks in advance

arnelgp
02-12-2022, 04:51 AM
you can add Title to you tables:


For i = 1 To ActiveDocument.Tables.Count
ActiveDocument.Tables(i).Title = i & ""
Next


so you can Step Into Code F8:

for i = 1 to ActiveDocument.Tables.Count
ActiveDocuments.Tables(i & "").Select
next

and you can see which tables are being selected.
you can then identify which "i" is to exclude from
emptying the cells.