Excel VBA:range.Match不断返回



我真的试图自己解决这个错误,但我似乎无法弄清楚它。我有一系列的单元格,我试图找到一个特定的值,即使我知道该值就在那里,而且我知道搜索值与该范围内的值匹配。

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)

到目前为止,我尝试过的一切都使RNG一无所有。我知道查找的某些输入值将延续到下一个呼叫中,因此我在此通话中重置它们,以确保我获得了正确的设置。我已经尝试设置一个IF比较年份(目标日期)和范围(" C1")。值,并评估为true。最奇怪的是,该代码至少对我有效,现在拒绝工作。我已经用尽了解决方案的所有想法,我知道这个问题非常具体,但希望有人可以提供帮助。如果您需要更多信息,请询问。

谢谢

此代码是从:http://www.rondebruin.nl/win/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

最新更新