vba code for filtering by latest date

Rudolf14

New Member
Joined
Oct 11, 2018
Messages
5
Hey guys,

I'm new to the VBA code thing...still in the record macro stage!!
I need assistance.

I have a worksheet that contains 6 columns,
A: Section
B: Machine
C: Component
D: Severity
E: Comment
F: Date.

Now I need a VBA code to filter by the latest date.:confused::confused:
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hey guys,

I'm new to the VBA code thing...still in the record macro stage!!
I need assistance.

I have a worksheet that contains 6 columns,
A: Section
B: Machine
C: Component
D: Severity
E: Comment
F: Date.

Now I need a VBA code to filter by the latest date.:confused::confused:

NB. I forgot to mention, I have a code for sorting out the data, Now I need a code for the filter part. Thanx in advanced
 
Upvote 0
Try this.

Code:
Sub FilterMaxDate()
Dim R As Range: Set R = Range("A1").CurrentRegion
Dim MaxDate As Date: MaxDate = Application.Max(R.Columns(R.Columns.Count))


R.AutoFilter Field:=6, Criteria1:=MaxDate
End Sub
 
Last edited:
Upvote 0
HI Irobbo314,

Thanx for the code. how ever I tried it but it keeps on giving me the date in the first row. You will see I edited the code..

Sub FilterMaxDate()
Dim R As Range: Set R = Range("F:F").CurrentRegion
Dim MaxDate As Date: MaxDate = Application.Max(R.Columns(R.Columns.Count))


R.AutoFilter Field:=6, Criteria1:=MaxDate
End Sub
 
Upvote 0
HI Irobbo314,

Thanx for the code. how ever I tried it but it keeps on giving me the date in the first row. You will see I edited the code..

Sub FilterMaxDate()
Dim R As Range: Set R = Range("F:F").CurrentRegion
Dim MaxDate As Date: MaxDate = Application.Max(R.Columns(R.Columns.Count))


R.AutoFilter Field:=6, Criteria1:=MaxDate
End Sub

And the other problem I have is that there is a lot of data on the machine that has the same date.
Here is a sample of what I have.
SectionMachineComponentSeverityDate
P33CM0147CM\BOOSTER\PUMPNORMAL2018/07/18
P33CM0147CM\RH\SEC\CON\REDRNORMAL2018/05/14
P33CM0147CM\RH\SEC\CON\REDRNORMAL2018/07/18
P33CM0147CM\RH\SEC\CON\REDRNORMAL2018/04/25
P33CM0147CM\RH\SEC\CON\REDRNORMAL2018/06/03
P33CM0147CM\L\RE\CUTT\GBXNORMAL2018/07/18
P33CM0147 CM\L\RE\CUTT\GBXNORMAL2018/06/03
P33CM0147CM\L\RE\CUTT\GBXNORMAL2018/05/14
P33CM0147CM\L\RE\CUTT\GBXNORMAL2018/04/25
P33CM0147CM\RH\PRIM\CON\REDRNORMAL2018/06/03
P33CM0147CM\RH\PRIM\CON\REDRNORMAL2018/07/18
P33CM0147CM\RH\PRIM\CON\REDR NORMAL2018/04/25
P33CM0147CM\HYDBORDERLINE2018/07/18
P33CM0147CM\HYDNORMAL2018/06/04
P33CM0147CM\HYDNORMAL2018/06/03
P33CM0147CM\HYDNORMAL2018/05/14
P33CM0147CM\HYDBORDERLINE2018/04/25
P33CM0147CM\L\GATH\ARMNORMAL2018/05/14
P33CM0147CM\L\GATH\ARMNORMAL2018/06/03

<colgroup><col span="4"><col></colgroup><tbody>
</tbody>

And this is what I want:
SectionMachineComponent Severity Date
P33CM0147CM\BOOSTER\PUMP NORMAL 2018/07/18
P33CM0147CM\RH\SEC\CON\REDR NORMAL 2018/07/18
P33CM0147CM\L\RE\CUTT\GBX NORMAL 2018/07/18
P33CM0147CM\RH\PRIM\CON\REDR NORMAL 2018/07/18
P33CM0147CM\HYD BORDERLINE 2018/07/18

<colgroup><col span="4"><col></colgroup><tbody>
</tbody>
 
Upvote 0
Assuming the dates are in column F :
Code:
Dim myDate As Date: myDate = Application.Max(Columns(6))
ActiveSheet.AutoFilterMode = False
Columns(6).AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [F2].NumberFormat)
 
Upvote 0
Assuming the dates are in column F :
Code:
Dim myDate As Date: myDate = Application.Max(Columns(6))
ActiveSheet.AutoFilterMode = False
Columns(6).AutoFilter Field:=1, Criteria1:="=" & Format(myDate, [F2].NumberFormat)
Dude!!!!! You are a ledgent....THANX a Mill!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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