Delete rows that don't match data from a list

jimgima

New Member
Joined
Sep 19, 2006
Messages
26
Let's say that I have two sheets on a spreadsheet.

Sheet1 has tons of data. In Column E of sheet1 are numbers that represent course numbers. I only want the data from 10 course codes to be listed (but there are hundreds on the spreadsheet).

On Sheet2 if I create a list of the course codes I want to be listed on Sheet1. Can I create a script to delete all of the rows that don't match the data on Sheet2?

Sheet2 only has one column of data. The data is in Column A. It lists the course code numbers that I want to keep?

I'd like to be able to add and delete numbers from Sheet2.

Sheet1 will be populated with new data everyweek. Most of that data is crap, so that is why I want to delete the unwanted information.

Is this enough information? Thanks for your help.

Jim
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

gaj104

Well-known Member
Joined
Nov 9, 2002
Messages
864
Think this should work.

If you have headings on your sheet 1 then change to for i = 1 to for i = 2

Code:
Sub RemRows()

Dim lRowA As Long
Dim lRowB As Long

Dim wsA As Worksheet
Dim wsB As Worksheet

Set wsA = Sheets("Sheet1")
Set wsB = Sheets("Sheet2")

lRowA = wsA.Range("E65336").End(xlUp).Row
lRowB = wsB.Range("A65336").End(xlUp).Row

For i = lRowA To 1 Step -1

    On Error Resume Next
        
        If wsB.Range("A1:A" & lRowB).Find(wsA.Range("E" & i).Value).Value = "" Then
        
        wsA.Rows(i).Delete
        
        End If
Next i

End Sub

Hope it helps
 

jimgima

New Member
Joined
Sep 19, 2006
Messages
26
Thanks

I'll try this as soon as I get to work. BTW I do have headings, so I'll follow your advice. Thanks again.
 

gaj104

Well-known Member
Joined
Nov 9, 2002
Messages
864
Slight edit,

I meant to say, as you have headings substitute:

Code:
For i = lRowA To 1 Step -1

for

Code:
For i = lRowA To 2 Step -1

Thanks
 

jimgima

New Member
Joined
Sep 19, 2006
Messages
26
Wow

That takes a long time to run. But, that's OK. I have a few ideas of how to make it run faster.

Here is the problem. I do have header rows. When I change

For i =1RowA To 1 Step -1

to

For i = 2RowA to step 1 -1

Am I missing something. I get a compile error: Expected: To

I'm a VB virgin.

Thanks.
 

Forum statistics

Threads
1,136,305
Messages
5,674,984
Members
419,540
Latest member
Nereus A

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
Top