View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Delete Rows based on not existing in range

Sub delrows()

Worksheets("sheet2").Activate
Sh2LastRow = Cells(Rows.Count, "A"). _
End(xlUp).Row
Set sh2names = Worksheets("sheet2"). _
Range(Cells(1, "A"), _
Cells(Sh2LastRow, "A"))

Worksheets("sheet1").Activate
Sh1LastRow = Cells(Rows.Count, "C"). _
End(xlUp).Row

RowCount = 1
Do While Not IsEmpty(Cells(RowCount, "C"))
MyName = Cells(RowCount, "C")
Set c = sh2names.Find(MyName, LookIn:=xlValues)

If c Is Nothing Then

Cells(RowCount, "C").EntireRow.Delete
Else
RowCount = RowCount + 1
End If

Loop


End Sub


" wrote:

After some help on this, can find examples on the forums where row
should be deleted based on X value. However, my requirement is:

Within Sheet1 contains x thousand rows, with data populated in up to
10 columns for each row. Based on Column C I need to delete any rows
within Sheet1 where the name in Column C is NOT EQUAL to a list of
names in Sheet2 Range Valid

For example

Sheet1
A: B: C: D: E:
123 Test Al Test Test
124 Test Mike Test Test
421 Test John Test Test
152 Test Al Test Test
242 Test Sarah Test Test
142 Test Tom Test Test


Sheet2
A:
Al
Mike
Tom

So in the above example Sarah and John don't appear within Sheet2's
range so I would want these lines deleting.

Appreciate any help as always.

Regards, Al.