'在黄色区域中查找值为2的单元格,并将这些单元格值变为5
Sub chaozhao3()
Dim rg As Range
Dim myaddress As String
Set rg = Range("a14:f24").Find(2)
If rg Is Nothing Then
Debug.Print "not 2"
Else
myaddress = rg.address
Do
rg.value = 5
Set rg = Range("a14:f24").FindNext(rg)
Loop While Not rg Is Nothing And rg.address <> myaddress 每次运行到这句代码就会报错:对象变量未设置
End If
End Sub
你的代码因为前边数字为2的单元格已经改成5了,所以后边查找不存在循环查找,就用不到address
改了一下你的代码
Sub chaozhao3()
Dim rg As range
Set rg = range("a14:f24").Find(2)
If rg Is Nothing Then
Debug.Print "not 2"
Else
Do
rg.Value = 5
Set rg = range("a14:f24").FindNext(rg)
Loop While Not rg Is Nothing
End If
End Sub
你的代码因为前边数字为2的单元格已经改成5了,所以后边查找不存在循环查找,就用不到address
改了一下你的代码
Sub chaozhao3()
Dim rg As range
Set rg = range("a14:f24").Find(2)
If rg Is Nothing Then
Debug.Print "not 2"
Else
Do
rg.Value = 5
Set rg = range("a14:f24").FindNext(rg)
Loop While Not rg Is Nothing
End If
End Sub
...
你的代码因为前边数字为2的单元格已经改成5了,所以后边查找不存在循环查找,就用不到address
改了一下你的代码
Sub chaozhao3()
Dim rg As range
Set rg = range("a14:f24").Find(2)
If rg Is Nothing Then
Debug.Print "not 2"
Else
Do
rg.Value = 5
Set rg = range("a14:f24").FindNext(rg)
Loop While Not rg Is Nothing
End If
End Sub
Sub findnum1()
Dim r As range, s As String
Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
If Not r Is Nothing Then
Do While Not r Is Nothing
r.Value = 5
Set r = Cells.Find(2, after:=r)
Loop
Else
MsgBox "未找到数字2"
End If
End Sub