返回列表 上一主題 發帖

[發問] 沒有設定物件變數或 With 區塊變數

本帖最後由 stillfish00 於 2014-6-9 13:39 編輯

回復 3# esperanto
說明範例的bug吧,我會把
  1. Loop While Not c Is Nothing And c.Address <> firstAddress
複製代碼
改成
  1.     If c Is Nothing Then Exit Do
  2. Loop While c.Address <> FirstAddress
複製代碼
原因應該是loop while 條件中,
VBA會把兩條件先求出來再And起來,造成
c.Address <> FirstAddress左式在c為nothing時求不出來。

TOP

回復 5# esperanto
應該是你在別的程式,在找到c後不會改掉c的值,所以loop中不會有c為nothing(找不到下一個)的問題。
而VBA說明範例
  1. With Worksheets(1).Range("a1:a500")
  2.     Set c = .Find(2, LookIn:=xlValues)
  3.     If Not c Is Nothing Then
  4.         FirstAddress = c.Address
  5.         Do
  6.             c.Value = 5
  7.             Set c = .FindNext(c)
  8.         Loop While Not c Is Nothing And c.Address <> FirstAddress
  9.     End If
  10.   End With
複製代碼
說明範例有c.value = 5 ,2都改為5後就找不到2了
而你的例子,有Range(Worksheets("Scheduling").Cells(c.Row, 1), Worksheets("Scheduling").Cells(c.Row + 1, 9)).ClearContents
會把你找的machine清掉,所以FindNext最後會傳回Nothing

TOP

        靜思自在 : 唯其尊重自己的人,才更勇於縮小自己。
返回列表 上一主題