2016-07-26 162 views
0

我是新來編寫VBA代碼,並且在線下面遇到了這個代碼,這與我正在嘗試做的非常相似。但是,當我嘗試運行它時,它會突出顯示Loop While Not c Is Nothing And c.Address <> firstAddress行,並彈出一條消息,提示「對象變量或塊變量未設置」。Excel VBA代碼

有誰知道如何解決這個問題?任何幫助深表感謝!

Sub Commodity() 
' 
' Commodity Macro 
' Commodity 
' 
' 
With Worksheets(1).Range("a1:a500") 

    Set c = .Find(2, LookIn:=xlValues) 
    If Not c Is Nothing Then 
     firstAddress = c.Address 
     Do 
      c.Value = 5 
      Set c = .FindNext(c) 
     Loop While Not c Is Nothing And c.Address <> firstAddress 
    End If 
End With 

End Sub 

回答

2

的問題還有就是你要尋找的2號和其更改爲5。一旦它改變了所有的人都將繼續從頂部循環中再次搜索,但c將等於什麼(如它無法找到任何東西)在第二次嘗試評估c.Address <> firstAddress時會導致錯誤。

當你要替換的人物我會刪除部分,只留下Loop While Not c Is Nothing

在另一方面,如果你不改變價值觀念,只是在尋找他們,你將需要包括c.Address <> firstAddress阻止它陷入無限循環。

+0

太棒了!感謝您的快速,有益的迴應:) –