Different result from match search and check direc

2019-06-14 16:48发布

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条回答
\"骚年 ilove
2楼-- · 2019-06-14 17:18

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)
查看更多
登录 后发表回答