2016-11-07 58 views
0

对于下面的问题,我错误地要求在C.R.中进行审查,但他们告诉我不是正确的地方,所以我向你提出同样的问题。从匹配搜索和直接检查的不同结果单元格()

我找不到答案。这个非常简单的代码返回2个不同的结果,为什么?!?!我会疯了...

Function FirstDay(t As String) As Boolean 

d = DateSerial(Left(t, 4), Mid(t, 5, 2), Right(t, 2)) - 1 
FirstDay = False 
Var = Application.Match(d, Worksheets("Diary").Columns(1), 0) 
If Not IsError(Var) Then 
    FirstDay = True 
End If 

'this only to check if direct match Val to cell() retrieve the same result 
If d = Sheets("Diary").Range("A12") Then Debug.Print "Yes" Else Debug.Print "no" 

End Function 

我打电话给我的函数FirstDay("20161107")我的范围sh("Diary").(columns(1))

(date,01/01/2016,07/02/2016,06/03/2016,03/04/2016,08/05/2016,05/06/2016,03/07/2016,07/08/2016,04/09/2016,02/10/2016,06/11/2016,04/12/2016) 

瓦尔应该是12,但我retreive Error2042然后FirstDay =假的,但如果我看到眼前的窗弦

If d = Sheets("Diary").Range("A12") Then Debug.Print "Yes" Else Debug.Print "no" 

检索 “是”

有人可以让我知道最新怎么追加?感谢Fabrizio

回答

1

Lookup_value可以是数字,文本或逻辑值。不是约会。所以将日期值转换为数字。

Var = Application.WorksheetFunction.Match(CDbl(d), Worksheets("Diary").Columns(1), 0) 
+0

rigth,另一种方式,我觉得它是设置“d”为龙,感谢 – Fabrizio