Excel VBA:Range.Match不会返回任何内容

我真的试图自己解决这个错误,但我似乎无法弄清楚。 我有一系列的单元格,我试图find一个特定的值,它不会返回任何东西,即使我知道值是在那里,我知道search值匹配范围内的值。

Dim targetDate As Date Dim rng As Range targetDate = Sheet1.Cells(3,3).Value 'the Year(targetDate) evaluates to 2015 and the search range has values 2015 at C1 and value 2016 at O1 'All the other cells are blank, unformatted, and the two values in the range are in general format Set rng = Sheet2.Range("A1:Z1").Find(What:=Year(targetDate), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False) 

我到目前为止所尝试的所有事情都是没有任何意义的。 我知道一些Find的input值会继续到下一个呼叫,所以我在这个呼叫中重置了它们,以确保我得到正确的设置。 我已经尝试设置一个if来比较Year(targetDate)和Range(“C1”)。Value并将其计算为true。 最奇怪的是,这个代码至less为我工作了几个macros的运行,现在拒绝工作。 我已经用尽了所有的想法解决scheme,我知道这个问题是非常具体的,但希望有人可以帮助。 如果您需要更多的信息,请询问。

谢谢

这段代码是从http://www.rondebruin.nl/win/s9/win006.htm改编的

 Sub Test() Dim FindString As String Dim rng As Range FindString = Format(Sheet1.Cells(3, 3).Value, "YYYY") With Sheets("Sheet1").Range("A1:Z1") Set rng = .Find(What:=FindString, _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not rng Is Nothing Then Application.Goto rng, True Else MsgBox "Nothing found" End If End With End Sub