Advanced Filter, xlFilterCopy and macro help

n0esc

New Member
Joined
Dec 19, 2005
Messages
15
Here is a sample of the data I am dealing with
testparkingdata.xls
ABCDEFGHIJKLMNOPQRS
1PERMIT#PERMIT COLOROLD PERMIT NUMBERFIRST NAMELAST NAMESUPERVISORCOLORMAKEMODELLICENSE NUMBERLICENSE PLATE STATEDATE OF VIOLATIONPARKING VIOLATION CODELOT LOCATIONBADGE # (OR INT.)DATE VOIDEDVOIDED BYOTHER INFORMATIONKEY COLUMN
21000GREENG3258JOHNDOEBOSS DOEBLACKGMCSIERRA 2500AAA 123IOWA
31001GREENG3351JOHNDOEBOSS DOEGRAYJEEPCHEROKEEBBB 123IOWA
41002GREENG5656JANEDOEJOHN DOEGOLDPONTIACGRAND PRIXCCC 123WISCONSIN
51003GREENJANEDOEJOHN DOEREDCHEVROLETSILVERADODDD 123WISCONSIN
61005GREENG5929JAMESDOEJANE DOEBLUEHONDACIVICEEE 123MINNESOTA
71006GREENG5819JAMESDOEJANE DOEBLUEVOLKSWAGENJETTAFFF 123MINNESOTA
FULL DATA



I need to be able to use a macro such as the one Norie posts or I located one here: http://www.contextures.com/excelfiles.html but I have been unable to make anything work.

I am using column S for the sorting. Any given cell may or may not be blank depending on missing information, or in the case of L:R if a ticket has never been given.

Rows will also appear multiple times if tickets were issued to that permit number. The only completely unique column will be A.

Ideally the macro would highlight cells on the main sheet if they are sorted to a subsheet, but I am not sure if that is possible. I can't even make it do the basic part that should work fine. :oops: :confused:

Code:
Option Explicit

Sub ExtractReps()
Dim ws1 As Worksheet
Dim wsNew As Worksheet
Dim rng As Range
Dim r As Integer
Dim c As Range
Set ws1 = Sheets("Sheet1")
Set rng = Range("Database")

'extract a list of Sales Reps
ws1.Columns("S:S").Copy _
  Destination:=Range("AL1")
ws1.Columns("AL:AL").AdvancedFilter _
  Action:=xlFilterCopy, _
  CopyToRange:=Range("AJ1"), Unique:=True
r = Cells(Rows.Count, "AJ").End(xlUp).Row

'set up Criteria Area
Range("AL1").Value = Range("S1").Value

For Each c In Range("AJ2:AJ" & r)
  'add the rep name to the criteria area
  ws1.Range("AL2").Value = c.Value
  'add new sheet (if required)
  'and run advanced filter
  If WksExists(c.Value) Then
    Sheets(c.Value).Cells.Clear
    rng.AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("Sheet1").Range("AL1:AL2"), _
        CopyToRange:=Sheets(c.Value).Range("A1"), _
        Unique:=False
  Else
    Set wsNew = Sheets.Add
    wsNew.Move After:=Worksheets(Worksheets.Count)
    wsNew.Name = c.Value
    rng.AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("Sheet1").Range("AL1:AL2"), _
        CopyToRange:=wsNew.Range("A1"), _
        Unique:=False
  End If
Next
ws1.Select
ws1.Columns("AJ:AL").Delete
End Sub
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function

If I try to run this, it resets the advanced filter to a much smaller area that matches the example sheet it came with, and only outputs the first 7 column headings, and no data rows. Other times it errors with 1004 mismatch.

Any help would be greatly appreciated. I'm lost, but after manually handling this data for 4 months and 2000 people, I need an easier way before a major mistake happens.[/code]
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,215,510
Messages
6,125,237
Members
449,217
Latest member
Trystel

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