Delete row for matched string

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
Hey all

So I have this code that looks down sheet 1 ,column A and if a match for the stated string is found then the coresponding row is deleted.

How can the code be changed so instead of me adding each 'string' to form part of the code , the code would check down a list held in column A of Sheet3. Each time a match is found to that list then the row is deleted back in sheet 1

VBA Code:
Sub DeleteRowWithContents()

    Sheets("Sheet1").Select
    Last = Cells(Rows.Count, "A").End(xlUp).Row
    For I = Last To 1 Step -1
        If (Cells(I, "A").Value) = "SALARY" Then
        Cells(I, "A").EntireRow.Delete
        Else
        If (Cells(I, "A").Value) = "WORKING TAX CREDIT" Then
        Cells(I, "A").EntireRow.Delete
        End If
        End If
        Next I
    End Sub

thanks in advance
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Replace you For I /Next I loop with
VBA Code:
    For I = Last To 1 Step -1
        If Application.WorksheetFunction.CountIf(Sheets("Sheet3").Range("A1:A100"), Cells(I, "A").Value) > 0 Then
            Cells(I, "A").EntireRow.Delete
        End If
    Next I
Then create the list in Sheet3!A1:A100, INCLUDING the item "SALARY" and run your macro

Bye
 
Upvote 0
Solution
Methinks you'll need nested loops; one for looping over the range of values from column A and one for looping over your values to be found (which I guess you already have working). I've been told here before not to refer to a sheet when looping as it's very inefficient, thus the suggestion to use an array (it's loaded once into memory).

This could build your array of values to look for, and rather than debug.print as I did, your inner loop would delete the row where the array value is found. When the outer loop exits, the inner loop would advance by one (i.e. pick the next word to look for). Replace "WORKING TAX CREDIT" with ary(i,1).

VBA Code:
Dim i As Integer, lastRow As Integer
Dim ary() As Variant

lastRow = Sheets("Sheet3").Cells(Rows.count, "F").End(xlUp).Row
ary = Range("F1:F" & lastRow)
For i = 1 To UBound(ary)
   Debug.Print ary(i, 1)
Next
 
Upvote 0
Replace you For I /Next I loop with
VBA Code:
    For I = Last To 1 Step -1
        If Application.WorksheetFunction.CountIf(Sheets("Sheet3").Range("A1:A100"), Cells(I, "A").Value) > 0 Then
            Cells(I, "A").EntireRow.Delete
        End If
    Next I
Then create the list in Sheet3!A1:A100, INCLUDING the item "SALARY" and run your macro

Bye
Perfect - thanks a lot
 
Upvote 0
Methinks you'll need nested loops; one for looping over the range of values from column A and one for looping over your values to be found (which I guess you already have working). I've been told here before not to refer to a sheet when looping as it's very inefficient, thus the suggestion to use an array (it's loaded once into memory).

This could build your array of values to look for, and rather than debug.print as I did, your inner loop would delete the row where the array value is found. When the outer loop exits, the inner loop would advance by one (i.e. pick the next word to look for). Replace "WORKING TAX CREDIT" with ary(i,1).

VBA Code:
Dim i As Integer, lastRow As Integer
Dim ary() As Variant

lastRow = Sheets("Sheet3").Cells(Rows.count, "F").End(xlUp).Row
ary = Range("F1:F" & lastRow)
For i = 1 To UBound(ary)
   Debug.Print ary(i, 1)
Next
Thanks for your reply
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top