【问题标题】:Different result from match search and check directly cell()匹配搜索的不同结果并直接检查 cell()
【发布时间】:2016-11-07 11:18:18
【问题描述】:

对于以下问题,我错误地要求在 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)

Var 应该是 12,但我检索 Error2042 然后 FirstDay = false,但如果我在即时窗口上看到字符串

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

检索“是”

有人可以告诉我什么是附加的吗?感谢法布里齐奥

【问题讨论】:

    标签: excel worksheet-function mach vba


    【解决方案1】:

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

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

    【讨论】:

    • rigth,我发现另一种方法是将“d”设置为 Long,谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2011-03-20
    • 2018-11-13
    相关资源
    最近更新 更多