2012-08-16 108 views
0

我試過了這個網站上的建議,似乎都沒有工作。繼續使用Excel查找函數獲取錯誤91

在單元格C6:Z6中,我的日期爲01/01/2011至01/12/2012(英國日期格式)。我運行下面的宏:

Sub FindDate() 

    Range("C6:Z6").Select 

    Selection.Find(What:="01/08/2012", After:=ActiveCell, LookIn:=xlFormulas _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Activate 

End Sub 

,總是似乎得到運行時錯誤91.我已經嘗試使用「設置」來設定的範圍內,並且沒有做,要麼。

對於上下文,我試圖獲取預設日期的列號(使用ActiveCell.Column)。

+0

你也可以做到使用worksheetfunction.match(...) – Dan 2012-08-16 09:51:54

回答

1

當您只是搜索「01/08/2012」時,您實際上是在搜索一個字符串。您必須使用CDate將其轉換爲日期。

此外,最好檢查是否使用If Not aCell Is Nothing Then找到任何東西以避免任何錯誤。看到我的帖子在這link

試試這個

Sub FindDate() 
    Dim aCell As Range 

    Set aCell = Range("C6:Z6").Find(What:=CDate("01/08/2012"), After:=ActiveCell, _ 
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 

    If Not aCell Is Nothing Then 
     MsgBox aCell.Column 
    Else 
     MsgBox "Not Found" 
    End If 
End Sub 
+0

大,它的工作原理吧!雖然我發現我必須將SearchOrder更改爲'xlByColumns',並將'After'設置爲'Range(「C6」)「以避免錯誤。 – Sputnik 2012-08-16 10:25:14