• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

how do I select the 2 oldest dates in vba

ayan1988

New Member
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, here is the code:


"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)"


Thanks
 
Hi, ayan1988!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


Despite of this, I found this issues:

a) At least the "End If" and "Next i" statements are missing at the end of the code for proper execution

b) If your worksheet has more than 65536 used columns from row 1 in advance, the code won't do nothing since .Row will be retrieving 1, and the For...Next loop won't execute. You can avoid this changing your 1st statement to:

For i=2 To Sheets("sheet1").Range("A1").End(xlDown).End(xlDown).End(xlUp).Row

c) How do you detect the 2 oldest dates at column D? You're not filter anything as you posted in your question, just sending mail when column D less or equal than 5 days from today

d) I couldn't see the whole code, but if you're not using it, I'd strongly recommend you to place as first line an "Option Explicit" clause, and then compile the code before running, from the VBA editor, Debug tab, Compile item.


Consider uploading a sample file (including manual examples of desired output), it'd be very useful for those who read this and might be able to help you. Thank you.

Give a look at the green sticky posts at this forums main page for uploading guidelines.


Regards!
 
Back
Top