Advance Filter, Find the Left-Overs

seekeagle

New Member
Joined
Mar 5, 2009
Messages
23
Assume that I have a database with 10 columns and that I know how to use an advance filter to copy selected lines to another worksheet based on the value in Column A. Assume that Column A is a list of names with Jane, Sue, ****, Harry, and about a dozen other people.

I would like to filter this table into five parts: (1) Jane, (2) Sue, (3) ****, (4) Harry, and (5) the Left-Overs. I know how to get lists 1 through 4 by using an advance filter. I also know how to use three rows of criteria to exclude 3 names. However, I do not know how to get List 5 where I need to exclude four (or more) names from the list.

I don't think this is possible directly on a worksheet, but I need to do it in VBA. Can anyone help? Is there a completely different approach to reach my objective? Your help will be appreciated.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
seekeagle,

You could copy your original data to a new sheet named "LeftOvers", and then delete all rows, where in column A are equal to Jane, Sue, ****, and Harry.
 
Upvote 0
seekeagle,

Sample data before the macro:


Excel Workbook
ABCDEFGHIJ
1Title ATitle BTitle CTitle DTitle ETitle FTitle GTitle HTitle ITitle J
2Jane
3Sue
4****
5Harry
6seekeagle
7Jane
8Sue
9****
10Harry
11Joseph
12Jane
13Sue
14****
15Harry
16Janet
17Jane
18Sue
19****
20Harry
21David
22Jane
23Sue
24****
25Harry
26Peter
27Jane
28Sue
29****
30Harry
31Hiker95
32Jane
33Sue
34****
35Harry
36AnotherName
37
Sheet1



After the macro:


Excel Workbook
ABCDEFGHIJ
1Title ATitle BTitle CTitle DTitle ETitle FTitle GTitle HTitle ITitle J
2seekeagle
3Joseph
4Janet
5David
6Peter
7Hiker95
8AnotherName
9
LeftOvers



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT+F11 to open the Visual Basic Editor
4. Press the keys ALT+I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT+Q to exit the Editor, and return to Excel

Code:
Option Explicit
Sub GetLeftOvers()
Dim LR As Long, a As Long
Dim NameArray() As Variant
Dim ws1 As Worksheet, ws2 As Worksheet
NameArray = Array("Jane", "Sue", "****", "Harry")
Set ws1 = Sheets("Sheet1")
On Error Resume Next
Sheets("LeftOvers").Select
If Err Then
  Worksheets.Add(After:=ws1).Name = "LeftOvers"
  Set ws2 = Sheets("LeftOvers")
Else
  Set ws2 = Sheets("LeftOvers")
  ws2.Cells.ClearContents
End If
On Error GoTo 0
With ws1
  LR = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A1:J" & LR).Copy ws2.Range("A1")
  Application.CutCopyMode = False
End With
With ws2
  With .Range("A1:A" & LR)
    For a = LBound(NameArray) To UBound(NameArray)
      .AutoFilter
      .AutoFilter Field:=1, Criteria1:=NameArray(a), Operator:=xlAnd
      .Offset(1).Resize(LR).EntireRow.Delete
      .AutoFilter
    Next a
    .Columns.AutoFit
  End With
End With
End Sub


Then run the "GetLeftOvers" macro.
 
Upvote 0
Hiker 95,

Thanks for your quick reply and coding. It looks really good, but it's late tonight, so I cannot try it until tomorrow. I will let you know how it works out. thanks again.

Seekeagle
 
Upvote 0

Forum statistics

Threads
1,215,112
Messages
6,123,162
Members
449,099
Latest member
afishi0nado

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