Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Delete Rows based on not existing in range

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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Delete Rows based on not existing in range

Al,

Try the macro below. This assumes that the list of valid values is actually a named range Valid

HTH,
Bernie
MS Excel MVP

Sub RemoveNotValid()
Dim myRow As Long
With Worksheets("Sheet1")
myRow = .Cells(.Rows.Count, 3).End(xlUp).Row
.Range("D1").EntireColumn.Insert Shift:=xlToRight
.Range("D1:D" & myRow).FormulaR1C1 = "=ISERROR(MATCH(RC[-1],Valid,FALSE))"
.Range("D1:D" & myRow).AutoFilter Field:=1, Criteria1:="TRUE"
.Range("D2:D" & myRow).SpecialCells(xlCellTypeVisible).EntireRow.D elete
.Range("D2:D" & myRow).AutoFilter
.Range("D2").EntireColumn.Delete
End With
End Sub

wrote in message ps.com...
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Delete Rows based on not existing in range

Thanks to you both for your help on this. Really appreciated.

Cheers, Al.

On 29 Jun, 13:10, "Bernie Deitrick" <deitbe @ consumer dot org wrote:
Al,

Try the macro below. This assumes that the list of valid values is actually a named range Valid

HTH,
Bernie
MS Excel MVP

Sub RemoveNotValid()
Dim myRow As Long
With Worksheets("Sheet1")
myRow = .Cells(.Rows.Count, 3).End(xlUp).Row
.Range("D1").EntireColumn.Insert Shift:=xlToRight
.Range("D1:D" & myRow).FormulaR1C1 = "=ISERROR(MATCH(RC[-1],Valid,FALSE))"
.Range("D1:D" & myRow).AutoFilter Field:=1, Criteria1:="TRUE"
.Range("D2:D" & myRow).SpecialCells(xlCellTypeVisible).EntireRow.D elete
.Range("D2:D" & myRow).AutoFilter
.Range("D2").EntireColumn.Delete
End With
End Sub



wrote in glegroups.com...
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 TestAlTest Test
124 Test Mike Test Test
421 Test John Test Test
152 TestAlTest 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.- Hide quoted text -


- Show quoted text -



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete rows based upon a range of times farmboy Excel Discussion (Misc queries) 6 October 16th 09 05:02 PM
Delete Rows based on value Sabosis Excel Worksheet Functions 4 October 28th 08 11:21 PM
copying specific rows to an existing sheet, based on user paramete Carlee Excel Programming 1 April 2nd 07 12:38 PM
Delete rows based on value... Gordon[_2_] Excel Programming 3 September 15th 06 09:14 PM
Add/Delete rows based on count Mark K[_6_] Excel Programming 7 August 21st 06 10:01 PM


All times are GMT +1. The time now is 10:23 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"