2012-06-07 185 views
3

所以,我有一個工作表,其中我想在範圍(「A:A」)中搜索Range的值(「M」& i)。然而,當我嘗試運行這段代碼,它返回一個錯誤:「運行時錯誤‘91’:對象變量或帶塊未設置當我點擊調試,它對象變量或塊未設置 -

SearchIn = Range("A:A") 

我發現了一個錯誤。爲什麼Google在互聯網和這個網站(找到了),但我還是解決不了問題。任何人都得到了線索?

Sub Find_Replace() 

Dim i As Integer 
Dim SearchIn As Range 
Dim SearchedObject As Range 
Dim FinalCell As Range 
Dim SumCell As Range 


i = 5 
SearchIn = Range("A1:A740") 
StartSearch = Range("A" & i) 
FinalCell = Range("N" & i) 

Do While i <= 740 

SearchedObject = SearchIn.Find(What:="M" & i, After:=StartSearch, LookIn:=xlValues,   LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 

If SearchedObject.Value = Range("M" & i).Value Then FinalCell = FinalCell.Value + SearchedObject.Offset(0, 5).Value 

Loop 


End Sub 

回答

5

我沒有測試過,但是這是你正在嘗試做的?

分配範圍時,必須使用SET

Sub Find_Replace() 
    Dim i As Integer 
    Dim SearchIn As Range 
    Dim SearchedObject As Range 
    Dim FinalCell As Range 
    Dim SumCell As Range 

    i = 5 

    Set SearchIn = Range("A1:A740") 
    Set StartSearch = Range("A" & i) 
    Set FinalCell = Range("N" & i) 

    Do While i <= 740 

     Set SearchedObject = SearchIn.Find(What:="M" & i, After:=StartSearch, _ 
     LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
     SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 

     If Not SearchedObject Is Nothing Then 
      If SearchedObject.Value = Range("M" & i).Value Then _ 
      FinalCell.Value = FinalCell.Value + SearchedObject.Offset(0, 5).Value 
     End If 
    Loop 
End Sub 

編輯:另外,建議使用全路徑否則搜索將始終處於活躍的片

發生例如

Set SearchIn = Sheets("Sheet1").Range("A1:A740") 

同樣爲別人着想。

+0

非常感謝你......解決了它。 :) – speci

相關問題