Consulting

Results 1 to 5 of 5

Thread: Automatically filter selected worksheets by date criteria

  1. #1
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location

    Automatically filter selected worksheets by date criteria

    Hello friends who help us.
    I turned the internet in search of a macro to filter my chosen worksheets in a workbook with over 90 worksheets on column A and criteria.
    I found some macros, but they filter absolutely all worksheets, and I only want selected from me (I mean in the macro to set the worksheet names)
    Here is my problem for which I ask for your help: I ​​have a workbook with over 90 worksheets. I want specific worksheets for example with names: "Paris total"; "London total"; "Germany total", etc. by pressing the button (for the macro) to display a window in which to write a month and a year (something => Application.InputBox (Prompt: = "please write the desired month and year" which are located in column A of each of the selected worksheets and filter them.
    If there is a second button to remove the selected criteria, it will be just perfect for the same selected worksheets.
    If you need a table for example - I will do. If you have questions, I will answer you.
    I know that you are unique and with countless opportunities and thank you cordially.
    2018-01-04_221502.jpg

  2. #2
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    With this macro I pass through my chosen worksheets and choose the desired month and year.
    Sub filterrrr()
    
        Sheets("Sheet5").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$51").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet6").Select
       
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$66").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet7").Select
        
        ActiveSheet.Range("$A$1:$Q$86").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet8").Select
        ActiveWindow.SmallScroll Down:=-71
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$51").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet9").Select
        
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$59").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet10").Select
        
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$176").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet11").Select
        
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$53").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet12").Select
        
        ActiveSheet.Range("$A$1:$Q$59").AutoFilter Field:=1, Operator:= _
            xlFilterValues, Criteria2:=Array(1, "12/1/2017")
        Sheets("Sheet5").Select
        Range("A1").Select
    End Sub
    And with this I'm trying to remove the selected criteria

    Sub remove()
        Sheets("Sheet5").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$51").AutoFilter Field:=1
        Sheets("Sheet6").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$66").AutoFilter Field:=1
        Sheets("Sheet7").Select
        ActiveSheet.Range("$A$1:$Q$86").AutoFilter Field:=1
        Range("A1").Select
        Sheets("Sheet8").Select
        ActiveSheet.Range("$A$1:$Q$51").AutoFilter Field:=1
        Sheets("Sheet9").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$59").AutoFilter Field:=1
        Sheets("Sheet10").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$176").AutoFilter Field:=1
        Sheets("Sheet11").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$53").AutoFilter Field:=1
        Sheets("Sheet12").Select
        Range("A1").Select
        ActiveSheet.Range("$A$1:$Q$59").AutoFilter Field:=1
        Sheets("Sheet5").Select
    End Sub
    Test silter selected sheets by date.xlsm

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by k0st4din View Post
    If there is a second button to remove the selected criteria, it will be just perfect for the same selected worksheets.
    Does this second button only remove filter from column A, or from all columns?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    try (see comments in the code):
    Sub filterrrr()
    mnth = Application.InputBox("Enter month", "Month")
    yr = Application.InputBox("Enter year", "Year")
    dt = mnth & "/1/" & yr
    For Each sht In Sheets(Array("Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9", "Sheet10", "Sheet11", "Sheet12"))
      sht.Range("A1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(1, dt)
    Next sht
    End Sub
    Sub remove()
    For Each sht In Sheets(Array("Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9", "Sheet10", "Sheet11", "Sheet12"))
      'only ONE of the next two lines:
      sht.Range("A1").AutoFilter Field:=1 'removes filter only from column A
      'sht.ShowAllData 'removes filters from all columns
    Next sht
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Again and as always you are unique.
    Everything works well.
    Thank you very much p45cal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •