Conditionally delete rows in currentliy selected table

Andreas Hermle
Andreas Hermle used Ask the Experts™
on
Dear Experts:

On the currently selected table I would like to perform the following actions using VBA

The macro is to loop thru the first column of the selected table and look for the term 'error' there. If found the whole row is to be deleted. Of course there could be many cells in the first column where this term 'error' occurs.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Do you want to do that with MS Excel or MS Access ?

With MS Excel, you can use the delete methode of the range object.
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set wb = ThisWorkbook
Set ws = wb.Worksheets(1)
ws.rows(5).Delete shift:=xlUp

Open in new window


With Access, you can use a query.
Andreas HermleTeam leader

Author

Commented:
Hi Farbrice

thank you very much for your quick help. I was posting his question on the Word for Windows Section :-)
Top Expert 2016
Commented:
Hi,

pls try
Sub macro()

Set tbl = Selection.Tables(1)
For idx = tbl.Columns(1).Cells.Count To 1 Step -1
    Set cel = tbl.Columns(1).Cells(idx)
    If Len(cel.Range.Text) = 7 Then 'error
        If InStr(1, cel.Range.Text, "error", vbTextCompare) Then
            tbl.Rows(cel.RowIndex).Delete
        End If
    End If
Next


End Sub

Open in new window

Regards
Andreas HermleTeam leader

Author

Commented:
Hi Rafalel,

works great as always :-)

Thank you very much for your superb help. I really appreciate it. Regards, Andreas

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial