2

I store items in a dictionary with the key being a date e.g. '2012/05/11' The key is then changed to a number (41040) by default. When I want to retrieve that item, I need to reference it by the number: dFRmonths.Item(41040). Since dFRmonths.Item("2012/05/11") does not exist.

I only have the date, not the number, so I need to convert the date to a number, then use the number to do the lookup.

Trying to convert a date to a number gives an 'overflow' error:

IntMonth = CInt("2012/05/11")

Any idea how to change "2012/05/11" into 41040 so I can look it up?

Thanks!

2
  • 1
    Max Integer vaue is 32767. Define IntMonth as Long, and use DateValue instead of CInt. Commented Sep 16, 2015 at 11:15
  • You can also overcome this problem by converting the date to a String before using it as a key. That way, the dictionary key preserves the 'yyyy/mm/dd' format. Commented Sep 16, 2015 at 12:08

1 Answer 1

3

A tiny trick!

Sub dural()
    IntMonth = CLng(CDate("2012/05/11"))
    MsgBox IntMonth
End Sub

This is because CDate() is REALLY flexible.

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! Realise now that the number exceeded the Integer limit (hence 'overflow' error) - I should've used Long from the start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.