Delete rows with specific token in Excel

I have a spreadsheet.  I want to delete rows within the spreadsheet when cell in column 6 of that row contain a specific token.  How will I write a Excel VBA to perform such task?
tommym121Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tommym121Author Commented:
I wrote the code a below, but does not seems to delete the row at all.  I am not sure why.

Sub DeleteRows1()
    'Deletes the entire row within the selection if the ENTIRE row contains no data.

    'We use Long in case they have over 32,767 rows selected.

    Dim i As Long
    Dim str As String
    Dim pos As Integer
    Dim str1 As String
    
    'We turn off calculation and screenupdating to speed up the macro.

     With Application
         .Calculation = xlCalculationManual
         .ScreenUpdating = False

         'We work backwards because we are deleting rows.

         For i = 9994 To 1 Step -1
'              If WorksheetFunction.CountA(Selection.Rows(i)) <> 0 Then
              If InStr(Cells(i, 6).Value, "TOKEN1") <> 0 Then
                     Selection.Rows(i).EntireRow.Delete
              End If
         Next i

        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
End Sub

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi tommy,

perhaps delete this line?
If WorksheetFunction.CountA(Selection.Rows(i)) <> 0 Then

if you want to keep it, you will need a closing End If

you are using
With Application

instead of specifying a particular sheet.  If you are using the activesheet, then use:

With Application.ActiveSheet

Open in new window


also, instead of:
For i = 9994 To 1 Step -1

you can figure out the last row like this:

dim nLastRow as long

 'using worksheet object
.cells(rows.count,1).end(xlup).row
nLastRow = .Cells(.Rows.Count, 1).End(-4162).Row  'xlUp=-4162

Open in new window


this:
              If InStr(Cells(i, 6).Value, "TOKEN1") <> 0 Then
                     Selection.Rows(i).EntireRow.Delete
              End If

should be

              If InStr(.Cells(i, 6).Value, "TOKEN1") <> 0 Then
                     .Cells(i, 6).EntireRow.Delete
              End If

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
actually, some application properties are being set and then the worksheet object is needed ... so:

     With Application
         .Calculation = xlCalculationManual
         .ScreenUpdating = False

         'We work backwards because we are deleting rows.
         with ActiveSheet
            For i = nLastRow To 1 Step -1
                'code
            next i
         end with 'sheet
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
please forgive me, I am new to this forum and can't figure out how to edit ... or even if I can.

After with ActiveSheet, you should figure out nLastRow, before the loop
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Hi Crystal,

You can edit, but only if your comment is the last one in the thread. To edit your comment, click the down facing arrow to the right of your name and select "Edit Comment".

Welcome to EE!

Scott
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
thank you, Scott  ~  and thanks for the welcome :)
tommym121Author Commented:
Thanks for the tips.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.