Solved

Find closest date in sorted vba collection

Posted on 2010-08-30
4
961 Views
Last Modified: 2012-05-10
Hello,

I have a sorted Collection containing dates (Excel VBA).  I need to find the closest date in the collection to a date 12 months prior to the current date.  Although the data is in a collection it can be put into another object such as an Array if that makes more sense to do this.

I'd appreciate any thoughts on the best way to do this.

Thanks.
0
Comment
Question by:tim_chamberlain
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 200 total points
ID: 33557802
Try the below function. To find the closest date to a date 12 months prior to today, you would use it like this....

    Dim dt As Date
    dt = FindClosestDate(DateAdd("m", -12, Date), coll)

Wayne
Function FindClosestDate(dt, coll As Collection) As Date
    Dim diff As Long: diff = 99999
    Dim closest As Date, d As Long
    For d = 1 To coll.Count
        If Abs(DateDiff("d", coll(d), dt)) < diff Then
            closest = coll(d)
            diff = Abs(DateDiff("d", coll(d), dt))
        End If
    Next
    FindClosestDate = closest
End Function

Open in new window

0
 

Author Closing Comment

by:tim_chamberlain
ID: 33557858
Perfect. Thanks very much Wayne.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33557878
Hi

90        Do While ie.busy Or Not ie.ReadyState = IE_READYSTATE.Complete: DoEvents: Loop

        htmlStr = ie.document.body.innerHTML
     
        pointer = InStr(1, htmlStr, "Future Events")
        pointer = InStr(pointer, htmlStr, "vAlign")
        htmlStr = Mid(htmlStr, pointer + 18, 9)
        Range("B5").Value = htmlStr

100       Set doc = ie.document

The additional lines between 90 & 100 passed the test on your first sample (A5), did not test it further, but I think it should work for all.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33557889
Apologies, wrong question, disregard my comment.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question