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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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.
[TABLE="width: 367"]
<colgroup><col span="4"><col></colgroup><tbody>[TR]
[TD]Section[/TD]
[TD]Machine[/TD]
[TD]Component[/TD]
[TD]Severity[/TD]
[TD]Date[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM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/TD]
[TD]CM0147[/TD]
[TD]CM\RH\PRIM\CON\REDR [/TD]
[TD]NORMAL[/TD]
[TD]2018/04/25[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD]BORDERLINE[/TD]
[TD]2018/07/18[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD]NORMAL[/TD]
[TD]2018/06/04[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD]NORMAL[/TD]
[TD]2018/06/03[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD]NORMAL[/TD]
[TD]2018/05/14[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD]BORDERLINE[/TD]
[TD]2018/04/25[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\L\GATH\ARM[/TD]
[TD]NORMAL[/TD]
[TD]2018/05/14[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\L\GATH\ARM[/TD]
[TD]NORMAL[/TD]
[TD]2018/06/03[/TD]
[/TR]
</tbody>[/TABLE]

And this is what I want:
[TABLE="width: 367"]
<colgroup><col span="4"><col></colgroup><tbody>[TR]
[TD]Section[/TD]
[TD]Machine[/TD]
[TD]Component[/TD]
[TD] Severity[/TD]
[TD] Date[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\BOOSTER\PUMP [/TD]
[TD] NORMAL [/TD]
[TD] 2018/07/18[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\RH\SEC\CON\REDR[/TD]
[TD] NORMAL[/TD]
[TD] 2018/07/18[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\L\RE\CUTT\GBX[/TD]
[TD] NORMAL[/TD]
[TD] 2018/07/18[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\RH\PRIM\CON\REDR[/TD]
[TD] NORMAL[/TD]
[TD] 2018/07/18[/TD]
[/TR]
[TR]
[TD]P33[/TD]
[TD]CM0147[/TD]
[TD]CM\HYD[/TD]
[TD] BORDERLINE[/TD]
[TD] 2018/07/18[/TD]
[/TR]
</tbody>[/TABLE]
 
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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