我的理解是这样的:
1、c没有定义类型,代码最前面应该加一句Dim c As Range
2、每个2都要改为5,在Do……Loop中
Do c.Value = 5 Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstAddress
当找到最后一个时,再查找时,c找不到了,这时c is nothing
由于两个条件是同时的Not c Is Nothing And c.Address <> firstAddress
则c没有属性address了,故也会产生错误
我改复杂了点,请看
Private Sub CommandButton1_Click() Dim c As Range Dim p As Boolean
With Worksheets(1).Range("a1:a500") Set c = .Find(2, LookIn:=xlValues) If Not c Is Nothing Then firstaddress = c.Address p = True Do c.Value = 5 Set c = .FindNext(c) If Not c Is Nothing Then If c.Address <> firstaddress Then p = True Else p = False End If Else p = False End If Loop While p End If End With End Sub
p=false的条件是,要么就找不到,要么就是找到之后且找到的地址与第一个地址一致 |