Macro to extract data to specified sheets using advanved Filter

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have data on Sheet "Imported Data in Cols A:V


I have branch code codes in Col V and have tried to write code to extract the data to sheets "BR 98150" ,"BR98151" "BR 98154" The criteria is in Z1:Z2 on these sheets

The 3 sheets are next to each other ("BR 98150" ,"BR98151" "BR 98154"

However when running the macro, I get a type Mismatch and the code below is highlighted


Code:
 Sheets("Imported Data").Range("A1:V1000").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Sheets(ws).Range("Z1:Z2"), CopyToRange:=Sheets(ws).Range("A1"), Unique:=False


See full code below



Code:
 Sub DataExtraction()

For Each ws In Sheets(Array("BR 98150", "BR 98151", "BR 98154"))
    With ws
    Sheets("Imported Data").Range("A1:V1000").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Sheets(ws).Range("Z1:Z2"), CopyToRange:=Sheets(ws).Range("A1"), Unique:=False
      End With
      
    Next ws
Application.ScreenUpdating = True

  
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You might want to consider using the Dim statement, it will make this sort of error more obvious.

eg it would be Dim ws as Worksheet

If ws is already a worksheet then Sheets(ws) doesn't make sense, it would need to be Sheets(ws.Name) which means you should just use ws.
You have a With ws statement but are not using it.

Rich (BB code):
Sub DataExtraction()

Dim ws As Worksheet

For Each ws In Sheets(Array("BR 98150", "BR 98151", "BR 98154"))

    ' Without using the With Statement
    Sheets("Imported Data").Range("A1:V1000").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=ws.Range("Z1:Z2"), CopyToRange:=ws.Range("A1"), Unique:=False
    ' OR ALTERNATIVE
    ' Using the With Statement
    With ws
        Sheets("Imported Data").Range("A1:V1000").AdvancedFilter Action:=xlFilterCopy, _
            CriteriaRange:=.Range("Z1:Z2"), CopyToRange:=.Range("A1"), Unique:=False
    End With
  
Next ws
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi Howard

another way would be to just use the array like
Code:
Array("BR 98150", "BR 98151", "BR 98154")
Code:
Sub DataExtraction_re()

Dim varString As Variant

Application.ScreenUpdating = False
For Each varString In Array("BR 98150", "BR 98151", "BR 98154")
    Sheets("Imported Data").Range("A1").CurrentRegion.AdvancedFilter _
            Action:=xlFilterCopy, _
            CriteriaRange:=Sheets(varString).Range("Z1:Z2"), _
            CopyToRange:=Sheets(varString).Range("A1"), _
            Unique:=False
   
Next varString
Application.ScreenUpdating = True

End Sub

Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

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