Sub Test()
Dim X As Long
X = Sheets("Sheet1").Range("I10").Value
y = Application.WorksheetFunction.Match(X, Sheets("Sheet1").Range("A1:Z1"), 0)
End Sub
As an alternative , you can also use a conversion function as follows :
Code:
Sub Test()
X = Sheets("Sheet1").Range("I10").Value
y = Application.WorksheetFunction.Match(CLng(X), Sheets("Sheet1").Range("A1:Z1"), 0)
End Sub
The problem is that if you do not explicitly declare the type of a variable , by default it is a Variant ; in this case , as a consequence of the assignment in the following statement :
X = Sheets("Sheet1").Range("I10").Value
X becomes a variable of Date type ; consequently , in the MATCH statement , the VBA Date type variable cannot match with the dates in the range A1:Z1 ( in which the values are just numbers ; remember that in Excel dates are actually numbers ).
Declaring the variable X as of type Long enables the MATCH to work , since the types of X and the dates in A1:Z1 now are identical.
Similarly , the forced conversion using the CLng function converts the Date type variable to one of Long type.