Is there any way to select two latest/oldest rows by date?

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Code:
[TABLE="width: 289"]
<colgroup><col span="3"><col></colgroup><tbody>[TR]
[TD]A  Cloumn`[/TD]
[TD]B Column[/TD]
[TD]C Column   [/TD]
[TD]D Column[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]52[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]53[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]4234[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]5232[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]42342[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]24442[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3/30/2018 0:00[/TD]
[/TR]
[TR]
[TD]Bad[/TD]
[TD="align: right"]41233[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
</tbody>[/TABLE]

Hi Guys, I am trying to select two latest and two oldest data in the dataset above.
Is there any easy function for selecting latest and oldest using vba?

Thank you!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Code:
[TABLE="class: cms_table, width: 289"]
<tbody>[TR]
[TD]A Cloumn`[/TD]
[TD]B Column[/TD]
[TD]C Column[/TD]
[TD]D Column[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]52[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2016 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]53[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]4234[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2016 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]5232[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]42342[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4/12/2018 0:00[/TD]
[/TR]
[TR]
[TD]Good[/TD]
[TD="align: right"]24442[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3/30/2018 0:00[/TD]
[/TR]
[TR]
[TD]Bad[/TD]
[TD="align: right"]41233[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]4/12/2017 0:00[/TD]
[/TR]
</tbody>[/TABLE]

My mistake. Dataset should've look like this. So I am trying to select two of latest dates (2018) and two of oldest (2016)

Thank you
 
Upvote 0
One approach...

Code:
Sub LatestOldest()
Dim rng As Range, rng2 As Range, r As Range
Set rng = Range("D1:D" & Cells(Rows.Count, 4).End(xlUp).Row)
For Each r In rng
    If r = WorksheetFunction.Large(rng, 1) Or _
        r = WorksheetFunction.Large(rng, 2) Or _
        r = WorksheetFunction.Small(rng, 1) Or _
        r = WorksheetFunction.Small(rng, 2) Then
        If Not rng2 Is Nothing Then
            Set rng2 = Application.Union(rng2, r)
        Else
            Set rng2 = r
        End If
    End If
Next r
rng2.EntireRow.Select
End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0
@jaik22:

Looks like you've got the answer -- courtesy by tonyyy.

If you need to select just the cells with the latest/oldest dates, remote the "EntireRow." from the code.
 
Upvote 0

Forum statistics

Threads
1,216,138
Messages
6,129,099
Members
449,486
Latest member
malcolmlyle

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