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




 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
 
Upvote 0
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
 
Upvote 0
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...
 
Upvote 0
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
 
Upvote 0
That sounds right.

I Hope you get it done. Any other questions, fee free...
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
You should double check your table name and column header name...
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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