我一直在閱讀相關的線程幾個小時,但似乎無法找到解決方案。非常感謝您的幫助,謝謝。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'塊內(注意點)。 –