1

I have a sheet named 'Anwendung' in a workbook where in column AI there are different fields based on which i am filtering. In this case it would be 'Anwendung'. For this i have managed to google and tweak certain section of the code and the code looks like this

Sub DynamicFilter()
SearchCol = "Typ"
SearchFor = "Anwendung"
Set rng1 = ActiveSheet.UsedRange.Find(SearchCol, , xlValues, xlWhole)
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=rng1.Column, Criteria1:=SearchFor
End Sub

Now I have a couple of other sheets in the workbook where i need to filter them according to other strings for e.g. 'Ressourcen' in column AI. With the above stated code i can just apply it for the active sheet and not the entire work book.

If I define Dim ws As Worksheet and then run a for loop in it. Then it filters all the sheets in the workbook with 'Anwendung' as filter criteria and for certain sheets the filter criteria is different.

Sub DynamicFilter()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
SearchCol = "Typ"
SearchFor = "Anwendung"
Set rng1 = ActiveSheet.UsedRange.Find(SearchCol, , xlValues, xlWhole)
Range("A1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=rng1.Column, Criteria1:=SearchFor
Next ws
End Sub

All i need is how can i define multiple filters for multiple sheets. Filter 1 'Anwendung' for a sheet 1 and Filter 2: 'Ressourcen' for sheet 2 as a filter for another sheet and so on.


Solved

Had to record the Macro, execute it and tweak it a bit.

Sheets("Anwendungen").Select
    ActiveWindow.SmallScroll ToRight:=15
    ActiveSheet.Range("$A$1:$AZ$10637").AutoFilter Field:=35, Criteria1:= _
        "Anwendung"
    Sheets("Ressourcen").Select
    ActiveWindow.SmallScroll ToRight:=16
    ActiveSheet.Range("$A$1:$AZ$10637").AutoFilter Field:=35, Criteria1:=Array( _
        "Gebäude", "IT-System", "Netz", "Raum"), Operator:=xlFilterValues
3
  • Where is the Anwendung originally stored? is it in a specific (same) cell on each worksheet or is that the worksheet's name?
    – user2140173
    Oct 27, 2014 at 15:21
  • @vba4all- Anwendung is the name of the sheet as well as stored as Anwendungen, resourcen, Netz etc in column AI. And i am trying to filter sheet name= Anwendung with the filter in column AI= Anwendung and similarly for ressourcen. Oct 27, 2014 at 15:39
  • @vba4all- Thank you very much. But it does exactly the same what i described earlier when i loop it using for. When i use the SearchFor.range= ws.Range("AI1") it filters for all the rows in the entire workbook with 'Anwendung'. Although i have a sheet with the name 'Ressourcen' and in column AI there is 'Resourcen' as a cell value. What i really need is 2 filters. Filter 1= Anwendung that i can run on Anwendung sheet. Filter 2= Ressourcen that i can rund on ressourcen sheet. Oct 27, 2014 at 15:57

1 Answer 1

0

If the criteria is the worksheet name all it takes is assigning the name to the SearchFor variable

Sub DynamicFilter()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        SearchCol = "Typ"
        SearchFor = ws.Name
        Set rng1 = ws.UsedRange.Find(SearchCol, , xlValues, xlWhole)
        ws.Range("A1").AutoFilter
        ws.Range("A1").AutoFilter Field:=rng1.Column, Criteria1:=SearchFor
    Next ws
End Sub

In case the criteria for the SearchFor is a cell you can assign the cell's value to that variable like this:

SearchFor = ws.Range("AI1")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.