I'm working on a macro that will search a List sheet for different counties and then paste the entire row onto the current sheet. I have a worksheet for each person (named Mark, John, etc.) and each person is assigned several counties. Mark has three counties, listed in cells J1:L1, which I've named as a range (MyCounties). My macro looks through Sheet "List" column "I" for each of those counties and copy the entire row onto Sheet "Mark" starting at "A4". The macro that I have works very well for that.
For larger areas like Los Angeles County though, it gets split up between 6 people, who each take different cities within that county, and within the city of LA itself, take different zip codes. Is it possible to search for matching county and then city (for all cities not LA), as well as for Los Angeles (city) and then zip code? County is column "I", City is column "G", and Zip is column "H". So "Andrew" would have within LA county cities of Alhambra, Arcadia, etc. and also LA (city) zip codes of 90004, 90006, etc. I know that the macro posted below won't work for this, but is there a way to edit it to make it do what I want? I have a helper sheet ("Los Angeles") that shows persons name (Peter) in A1:D1 (Merged Cells), B3 and down are Counties to filter by, C3 and down are cities to filter by, and D3 and down are zip codes to filter by. Then we skip a column, and F1:I1 is next person.
I have a test document that I'll attach as soon as I figure out how to do that. test doc
Sub MoreReports()
Dim w As Long, cVar As Variant, zVar As Variant, rw As Long, sDoc As Worksheet, tDoc As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set sDoc = Sheets("List")
Set tDoc = Sheets("Peter")
Set cVar = Sheets("Los Angeles").Range("C3:C52")
Set zVar = Sheets("Los Angeles").Range("d3:d52")
With sDoc
If .AutoFilterMode Then .AutoFilterMode = False
With .Range(.Cells(4, "G"), .Cells(.Rows.Count, "I").End(xlUp))
.AutoFilter field:=3, Criteria1:="Los Angeles", Operator:=xlAnd
.AutoFilter field:=1, Criteria1:=cVar, Operator:=xlFilterValues
.AutoFilter field:=1, Criteria1:="Los Angeles", Operator:=xlAnd
.AutoFilter field:=2, Criteria1:=zVar, Operator:=xlFilterValues
.AutoFilter field:=3, Criteria1:="Ventura", Operator:=xlFilterValues
With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
If CBool(Application.Subtotal(103, .Cells)) Then
.Cells.EntireRow.Copy Destination:=tDoc.Cells(rw, "A")
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
So, I'm going to try to give one example for this (Thomas). Note that I am looking at the city/zip only, as the county should be inclusive of these two unique values; some cities can share the same zip.
The overall process I intend to follow is:
.1) Remember the range on the reference sheet (I do this so I can set my matches up correctly)
.2) Remember the range on the List sheet
.3) Set-up a loop for sorting through Thomas' references
.4) Within the loop, create another loop to search the values on the List
.5) Use an If-statement to check the condition
.6) If conditions are met, then we will cut and paste the row to the appropriate sheet
.7) Close the If-statement
.8) Go to the next j in the loop
.9) Go to the next i in the loop
.10) Filter Thomas' sheet by Zip code (should remove all empty rows)
With this one example, you should be able to set-up each other person's sheets!
Altogether, it looks like (there may be better ways, but this was the easiest of the Match() things I could think of, which in my head seemed easier than the Find() alternative):