• 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.

Using MATCH with Dates

polarisking

Member
The following seven date values are contained in Column A rows 1 through 7
7/25/2016
7/26/2016
7/27/2016
7/28/2016
7/29/2016
7/30/2016
7/31/2016

I'm getting stuck using MATCH within VBA using Date lookups. Keeps coming back with a 1004 - not found message. Change the values to animal names and the match argument to one of the names, and it works fine. I must not be handling the date properly, but I cannot figure out what the problem is.

Code:
Sub Chandoo()

    Debug.Print WorksheetFunction.Match(CDate("7/28/2016"), Range("A1:A7"), 0)

End Sub
 
Since Excel stores date in value (ex: 42579 for 7/28/2016) you need convert CDate to Long or Double. So something like below should work.

Code:
Sub test()
Debug.Print WorksheetFunction.Match(CLng(CDate("7/28/2016")), Range("A1:A7"),0)
End Sub
 
Back
Top