Auto Filtering Issues with VBA

BKennelly

New Member
Joined
May 28, 2018
Messages
6
Essentially I am trying to filter Column Z by the months shown in cells B2:B4 upon the click of a button. So let's say that I have May through July showing in Cells B2:B4, once I click a button I want to Auto Filter Column Z to show only the rows that have dates between May and July. I know I could just use the normal filters, however, by doing it through VBA I am thinking that it will not matter which months are showing in B2:B4 and will pull the appropriate data. It sounds simple enough but I am clearly missing something, I have tried a multitude of things across the web and have had no luck so far. Not sure if it matters but I did use a table.

If you need any more information let me know.

Any help would be greatly appreciated.

Thank you,
Brandon




 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
Hi Brandon,

Welcome to the MrExcel Forum.

This code will filter a table named "Table5", where the 7th Column of the table is Column Z, for all dates between Cells B2 & B4. I know that is not exactly what you asked for but if Cell B3 falls between Cells B2 & B4, it should work.

Or at least I hope it get you closer to where you want to be...

Code:
Sub FilterTable()


    Range("Table5[[#Headers],[Column7]]").Select
    ActiveSheet.ListObjects("Table5").Range.AutoFilter Field:=7, Criteria1 _
        :=">=" & Range("B2"), Operator:=xlAnd, Criteria2:="<=" & Range("B4")
        
End Sub
 

BKennelly

New Member
Joined
May 28, 2018
Messages
6
Hello,

Thank you for the reply. I tried what you said however, I keep getting a Run-Time Error '1004': Application-defined or object-defined error.

I did tweak it a smidgen to match my excel doc. Instead of your "Table5" I have "Table3". As for the column # I am little confused on that one, wouldn't Column Z be the 26th column? I did try the code both ways and it neither worked. I assumed the #Headers should be my actual Header name (#Ack. Date) there.

Code:
Sub JobsForMontsAbove()



    Range("Table3[[#Ack. Date],[Column26]]").Select
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=26, Criteria1 _
        :=">=" & Range("B2"), Operator:=xlAnd, Criteria2:="<=" & Range("B4")
        
End Sub
The link for the file is below if it helps any:
https://drive.google.com/file/d/1Lxd8Ek09-R7BP62Yi1wOFBDwtFawsFID/view?usp=sharing


Thank you,
Brandon
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
The table name should be right with Table3. The column number is the number of the column in the actual table. So if in your Table3 the very first column of the table was Column Y (Column 1), then Column Z would be Column 2.

I hope that makes sense...
 

BKennelly

New Member
Joined
May 28, 2018
Messages
6
Ok, that makes sense now. My first column in Table3 is Column A, therefore, Column Z would be Column 26. Now to make sense of the code for this thing. :)

Thank you for you help thus far,
Brandon
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
That sounds right.

I Hope you get it done. Any other questions, fee free...
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
Hi Brandon,

Just to be clear. According to the info you provided (I am not big on clicking links), this code should work for you.


Code:
Sub FilterTable()


    Range("Table3[[#Headers],[Column26]]").Select
    ActiveSheet.ListObjects("Table5").Range.AutoFilter Field:=26, Criteria1 _
        :=">=" & Range("B2"), Operator:=xlAnd, Criteria2:="<=" & Range("B4")
        
End Sub
I hope this helps.
 
Last edited:

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
Yikes, I should just go back to bed...

Try this instead. Sorry about the confusion...

Code:
Sub FilterTable()


    Range("Table3[[#Headers],[#Ack. Date]]").Select
    ActiveSheet.ListObjects("Table3").Range.AutoFilter Field:=26, Criteria1 _
        :=">=" & Range("B2"), Operator:=xlAnd, Criteria2:="<=" & Range("B4")
        
End Sub
 

BKennelly

New Member
Joined
May 28, 2018
Messages
6
Thank you for all or your effort. It seems to be hanging up at the first line after Sub... where I am getting a Run-time error 1004 application-defined or object-defined error every time I try to step through the code.

Thank you,
Brandon
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,447
Office Version
365, 2010
Platform
Windows
You should double check your table name and column header name...
 

Forum statistics

Threads
1,082,318
Messages
5,364,524
Members
400,804
Latest member
davileal

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top