Delete Rows based on values in a separate sheet

caribou48

New Member
Joined
May 26, 2011
Messages
5
Hello ,


I am a complete VBA rookie, but a pretty advanced excel user.
I am trying to work a macro that will delete rows in a sheet based on
exclusions I have defined on another sheet.

worksheet

Columns
A B C D E F G H I

With varying number of rows depending on the report I am running. but in the region of 500


exclusion Sheet contains

Column
A
with 405 rows of exclusions, which are specific words


I want the macros to look through the worksheet and delete any row that contains a word that appears on the exclusion list.

I am getting a 424 error. I am sure I have made a pigs ear of this , This is probably my first serious attempt to code VBA I have pieced the code together from other posts I have seen. and my very limited knowledge.
.
Any help is greatly appreciated.

CODE:

Sub CCSExclusions()

Dim rngFound As Range, rngToDelete As Range
Dim strFirstAddress As String
Dim varList As Variant
Dim lngCounter As Long

Application.ScreenUpdating = False

varList = Range("Exclusions!A1:A405").Value

For lngCounter = LBound(varList) To UBound(varList)

With ActiveSheet.Range("A:I")
Set rngFound = .Find( _
What:=varList(lngCounter, 1), _
Lookat:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True _
)


If Not rngFound Is Nothing Then
If rngToDelete Is Nothing Then
Set rngToDelete = rngFound
Else
Set rngToDelete = Application.Union(rngToDelete, rngFound)
End If

strFirstAddress = rngFound.Address
Set rngFound = .FindNext(After:=rngFound)

Do Until rngFound.Address = strFirstAddress
Set rngToDelete = Application.Union(rngToDelete, rngFound)
Set rngFound = .FindNext(After:=rngFound)
Loop
End If
End With
Next lngCounter

If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete

Application.ScreenUpdating = True

End Sub


I am getting an error with

If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
????
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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