问题描述
在单元格C6:Z6中,我有日期01/01/2011至01 / 12/2012(英国日期格式)。我运行以下宏:
Sub FindDate()
范围(C6:Z6) 。选择
Selection.Find(什么:=01/08/2012,之后:= ActiveCell,LookIn:= xlFormulas _
LookAt:= xlPart,SearchOrder = = xlByRows,SearchDirection := xlNext,_
MatchCase:= False,SearchFormat:= False)。激活
End Sub
并且总是似乎得到运行时错误91.我尝试使用'Set'设置范围,也不会这样做。
对于上下文,我试图获取预设日期的列号(使用ActiveCell.Column)。
当您搜索01/08/2012时,您实际上正在搜索一个字符串。您必须使用 CDate
将其转换为日期。
最好检查是否发现有什么使用如果不是aCell是没有,然后
以避免任何错误。请参阅我在此中的信息。
尝试这个
Sub FindDate()
Dim aCell As Range
设置aCell =范围(C6:Z6)。Find(What:= CDate(01/08/2012),之后:= ActiveCell,_
LookIn:= xlFormulas,LookAt := xlPart,SearchOrder:= xlByRows,_
SearchDirection:= xlNext,MatchCase:= False,SearchFormat:= False)
如果不是aCell是没有,然后
MsgBox aCell。列
Else
MsgBoxNot Found
End If
End Sub
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).
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
这篇关于继续使用Excel查找功能获取错误91的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!