Keep getting Error 91 with Excel Find function

∥☆過路亽.° 提交于 2019-11-26 23:42:52

问题


I've tried the suggestions on this site and none seem to work.

In cells C6:Z6, I have dates 01/01/2011 through to 01/12/2012 (in UK date format). I run the following macro:

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

and always seem to get Run-time error 91. I've tried using 'Set' to the set the range and that doesn't do it either.

For context, I'm trying to obtain the column number for a preset date (using ActiveCell.Column).


回答1:


When you simply search for "01/08/2012", you are actually searching for a string. You have to convert it to a date using CDate.

Also it is better to check if anything is found using If Not aCell Is Nothing Then to avoid any errors. See my post in this link.

Try this

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


来源:https://stackoverflow.com/questions/11984404/keep-getting-error-91-with-excel-find-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!