我一直在阅读相关的线程几个小时,但似乎无法找到解决方案。非常感谢您的帮助,谢谢。Excel vba:.find函数返回运行时错误91
我想找到一个范围内的最大值,然后找到它所在的行。 此代码适用于我的第一个600多行数字,然后崩溃,并给我运行时错误91.它似乎总是崩溃不管我做什么,都在同一个地点。
Dim rSearchRange As Range
Dim dMaxToFind As Double
Dim rSolutionrange As Range
Set rSearchRange = Sheets("MySheet").Range(Cells(672, 1), Cells(681, 1))
With Application.WorksheetFunction
dMaxToFind = .Max(rSearchRange)
End With
'This bit here returns "nothing" even though i found the max value in this range
Set rSolutionrange = rSearchRange _
.Find(What:=dMaxToFind, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
看着我的数据,它看起来像它改变了这些行的格式?
Row 670 - 0.000458587
Row 671 - 0.000458587
Row 672 - 9.80465E-05
Row 673 - 9.80465E-05
编辑:
dMaxToFind返回9.80465352566588E-05
细胞公式返回0.0000980465352566588
它是在细胞范围之间的相同的值(将细胞(672,1),将细胞( 681,1)
好像vba不能理解那两个是一样的吗?
个谢谢,
马克
试试'...看着:= xlValues ...'' – user3598756
将.Find'如果没有匹配搜索条件返回'Nothing'。当你稍后*使用'rSolutionRange'对象变量并且假设它被设置为有效引用('Nothing'不是有效的对象引用)时,错误91被*引发*(不是“返回”) - 您需要检查'如果rSolutionRange是Nothing,则在使用它之前。 –
如果'Sheets(“MySheet”)'不是活动工作表,'Sheets(“MySheet”)。Range(Cells(672,1),Cells(681,1))''会爆炸,因为'Cells xxx,yyy)'调用隐式引用'ActiveSheet',所以你也需要限定这些调用;使用'With Worksheets(“MySheet”)'然后'Set rSearchRange = .Range(.Cells(672,1),.Cells(681,1))'在'With'块内(注意点)。 –