how do i define the oldest 2 dates in vba

ayansen

New Member
Joined
Jan 22, 2013
Messages
2
Hi everyone,

I am new to Excel programming and I have a question regarding finding the oldest 2 dates in a specific cell.

I am trying to build an application that automatically sends an email but can't seem to filter the 2 oldest dates which is why when I click on send, all the records are sent.

Please look at the my code and tell me what the problem is. Thanks

For i = 2 To Sheets("sheet1").Range("f65536").End(xlUp).Row
If Cells(i, 4).Value <= Date + 5 Then
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I'm not sure how your code is intended to find the two oldest dates, I suppose you have other code and you are looping through.

Once you know your range you can use the small function to find the two oldest dates, example below.

Dim oldestDate, secondDate As Date
oldestDate = Application.WorksheetFunction.Small(ActiveSheet.Range("C1:C22"), 1)
secondDate = Application.WorksheetFunction.Small(ActiveSheet.Range("C1:C22"), 2)
 
Upvote 0
Hey thanks a lot for your reply. Tho I tried it but it didn't work. Can I send you the file and then maybe you will have a better vision of that?

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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