- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
2#
發表於 2014-2-9 18:00
| 只看該作者
回復 1# zxcxz - Option Explicit
- Sub Ex()
- Dim C As Range, firstAddress As String
- With Worksheets(1).Range("a1:a500")
- .Parent.Select
- Set C = .Find(2, LookIn:=xlValues) '[a1:a500] 搜尋 2
- If Not C Is Nothing Then
- firstAddress = C.Address
- Do
- C.Value = 5 '一直地將2換置為5,最後再也沒2了
- Set C = .FindNext(C)
- '搜尋 [a1:a500] 下一個 2
- C.Select '沒有2時 C Is Nothing '--> C.Address 錯誤沒有設定變數
- Loop While Not C Is Nothing 'And C.Address <> firstAddress
- End If
- End With
- End Sub
- Sub Ex1()
- Dim C As Range, firstAddress As String
- With Worksheets(1).Range("a1:a500")
- Set C = .Find(2, LookIn:=xlValues) '[a1:a500] 搜尋 2
- If Not C Is Nothing Then
- firstAddress = C.Address '
- Do
- '不執行 C.Value = 5
- Set C = .FindNext(C) '搜尋 [a1:a500] 下一個 2
- MsgBox C.Address <> firstAddress
- '終會回到第一2 -> C.Address = firstAddress '離開迴圈
- Loop While C.Address <> firstAddress '條件成立一直執行迴圈
- End If
- End With
- End Sub
複製代碼 |
|