返回列表 上一主題 發帖

[發問] Error 'Object variable or With block variable not set'

回復 1# zxcxz
  1. Option Explicit
  2. Sub Ex()
  3.     Dim C As Range, firstAddress As String
  4.     With Worksheets(1).Range("a1:a500")
  5.     .Parent.Select
  6.     Set C = .Find(2, LookIn:=xlValues)  '[a1:a500] 搜尋 2
  7.     If Not C Is Nothing Then
  8.         firstAddress = C.Address
  9.         Do
  10.             C.Value = 5  '一直地將2換置為5,最後再也沒2了
  11.             Set C = .FindNext(C)
  12.             '搜尋 [a1:a500] 下一個 2
  13.             C.Select  '沒有2時 C Is Nothing '--> C.Address 錯誤沒有設定變數
  14.         Loop While Not C Is Nothing 'And C.Address <> firstAddress
  15.     End If
  16. End With
  17. End Sub
  18. Sub Ex1()
  19.     Dim C As Range, firstAddress As String
  20.     With Worksheets(1).Range("a1:a500")
  21.         Set C = .Find(2, LookIn:=xlValues)  '[a1:a500] 搜尋 2
  22.         If Not C Is Nothing Then
  23.             firstAddress = C.Address   '
  24.             Do
  25.                 '不執行 C.Value = 5
  26.                 Set C = .FindNext(C)        '搜尋 [a1:a500] 下一個 2
  27.                 MsgBox C.Address <> firstAddress
  28.                 '終會回到第一2 -> C.Address = firstAddress  '離開迴圈
  29.             Loop While C.Address <> firstAddress  '條件成立一直執行迴圈
  30.         End If
  31.     End With
  32. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 【停滯不前,終無所得】人都迷於尋找奇蹟,因而停滯不前;縱使時間再多、路再長,也了無用處,終無所得。
返回列表 上一主題