VBA to add filter and filter for latest date

pscofe

New Member
Joined
Jan 24, 2020
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi, I've used VBAcode form other message boards to try and get this to work however it's only adding the filter to column A when I need the filter on for the entire top row ( I also want to filter column E if it contains "Receive") . In column A is a list of dates but I only want to see the Latest Date (won't always be yesterday, could be 4 or 5 days ago) Can someone help me amend this? Thanks

Sub PSRpt()

Dim myDate As Date

myDate = Application.Max(Columns(1))

ActiveSheet.AutoFilterMode = False
Columns(1).AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [A2].NumberFormat)
Worksheets("PS Report").Range("B:B,C:C,F:H,K:Q,T:U,AF:AG,AK:AN,AP:AQ").EntireColumn.Hidden = True
Selection.Copy
Range("AR1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Comments"
Range("AQ1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False



End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Your issue with it only filtering column A is with this part here:
Rich (BB code):
Columns(1).AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [A2].NumberFormat)
The part in red is telling it the range to filter, which you currently only have set to the first column. Change this to the entire range you want to filter.

Note that when you want to get VBA code like this, you can often use the Macro Recorder.
If you turn on the Macro Recorder and record yourself manually doing what you want the code to do, it will give you must of the code that you need.
You may need to clean it up a little, or make it more dynamic (the Macro Recorder is very literal), but it will give you the building blocks you need to get started.
 
Upvote 0
Thanks. I've tried at amending to "Range("A1:AQ").AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [A2].NumberFormat)" but that hasn't worked.
 
Upvote 0
Please show us an example of your data and expected output.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
I have a list of headers, I want to apply a filter across the top row and in column A to have the filter on latest date (which isn't always previous business day), thanks

1683895484287.png
 
Upvote 0
OK, to fix those filters, try replacing this part of the beginning of your code:
VBA Code:
Sub PSRpt()

Dim myDate As Date

myDate = Application.Max(Columns(1))

ActiveSheet.AutoFilterMode = False
Columns(1).AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [A2].NumberFormat)
with this:
VBA Code:
Sub PSRpt()

Dim myDate As Date
Dim rng As Range

myDate = Application.Max(Columns(1))

'Find current region of range starting with cell A1
Set rng = ActiveSheet.Range("A1").CurrentRegion

rng.AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [A2].NumberFormat)
rng.AutoFilter Field:=5, Criteria1:="receive"
 
Upvote 0
Solution
yes! That's worked perfectly, thank you for your help and this has massively helped my VBA knowledge (or lack of!)
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

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