vba 使用 Excel 查找功能不断收到错误 91
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11984404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep getting Error 91 with Excel Find function
提问by Sputnik
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:
在单元格 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
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)。
回答by Siddharth Rout
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
.
当您简单地搜索“01/08/2012”时,您实际上是在搜索一个字符串。您必须使用 将其转换为日期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.
此外,最好检查是否发现任何内容If Not aCell Is Nothing Then
以避免任何错误。请参阅此链接中我的帖子。
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