tommym121
asked on
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?
hi tommy,
perhaps delete this line?
If WorksheetFunction.CountA(S election.R ows(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:
also, instead of:
For i = 9994 To 1 Step -1
you can figure out the last row like this:
this:
If InStr(Cells(i, 6).Value, "TOKEN1") <> 0 Then
Selection.Rows(i).EntireRo w.Delete
End If
should be
perhaps delete this line?
If WorksheetFunction.CountA(S
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
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
this:
If InStr(Cells(i, 6).Value, "TOKEN1") <> 0 Then
Selection.Rows(i).EntireRo
End If
should be
If InStr(.Cells(i, 6).Value, "TOKEN1") <> 0 Then
.Cells(i, 6).EntireRow.Delete
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
After with ActiveSheet, you should figure out nLastRow, before the loop
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
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
thank you, Scott ~ and thanks for the welcome :)
ASKER
Thanks for the tips.
ASKER
Open in new window