简体   繁体   English

使用Excel查找功能不断收到错误91

[英]Keep getting Error 91 with Excel Find function

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). 在单元格C6:Z6中,我具有2011年1月1日至2012年12月12日的日期(以英国日期格式)。 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. 并且似乎总是会出现运行时错误91。我尝试使用“设置”来设置范围,但也没有这样做。

For context, I'm trying to obtain the column number for a preset date (using ActiveCell.Column). 对于上下文,我正在尝试获取预设日期的列号(使用ActiveCell.Column)。

When you simply search for "01/08/2012", you are actually searching for a string. 当您简单地搜索“ 01/08/2012”时,实际上是在搜索字符串。 You have to convert it to a date using CDate . 您必须使用CDate将其转换为日期。

Also it is better to check if anything is found using If Not aCell Is Nothing Then to avoid any errors. 另外,最好使用If Not aCell Is Nothing Then检查是否找到了If Not aCell Is Nothing Then以避免任何错误。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM