Different result from match search and check directly cell()

大兔子大兔子 提交于 2020-01-03 05:12:51

问题


For the below problem I mistakenly asked for a review in C.R., but they told me not to be the right place, so I extend to you the same question.

I cannot find answers alone. this very simple code return 2 different results, why ?!?! I'll be crazy...

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

I call my function with FirstDay("20161107") my range in sh("Diary").(columns(1)) are

(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)

Var should be 12 but I retreive Error2042 then FirstDay = false, but if I see on immediate windows the string

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

retrieve "Yes"

Can someone let me know whats append ? thank Fabrizio


回答1:


The Lookup_value can be a number, a text, or a logical value. Not a date. So convert the date value to a number.

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


来源:https://stackoverflow.com/questions/40463891/different-result-from-match-search-and-check-directly-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!