I\'m having problem making the match() work in excel VBA. The code is:
x = Application.Match(\"Sep 2008\", Range(\"F1:F1\"), 0)
The value i
Bottom line:
use WorksheetFunction.Match(CDbl(date), range, 0)
Alternatively, use a Date
cell's Value2
property (which will also be a Double
) instead of Value
for the search key.
CLng
suggested in other answers would discard the time part of the date
.
The same problem exists for the Currency
data type but you can't use CDbl
for it (see below for options).
Range.Value2 Property (Excel) article suggests that Date
and Currency
types are "special" in that they have an "internal representation" that's in stark contrast with displayed value. Indeed:
Apparently, Match
compares these internal values for performance reasons. So, we must ensure that they, rather than the readable representations, match exactly.
Since Date
is already floating-point internally, CDbl(date)
doesn't actually change the data.
For the Currency
type, CDbl
does change data, so it's out of question. So either