【发布时间】:2020-11-18 21:07:11
【问题描述】:
我需要在 A1:A14 范围内找到值为“b”的单元格。该范围内有三个这样的值。
如果找到值,则将 B2 设置为“是”,然后在下方偏移一行。
在这段代码中,我写的 findNext 函数不起作用。
Sub sinep()
Dim sinepRng As Range
Dim rangeRover As Range
Set rangeRover = Range("B2")
Set sinepRng = Range("A1:A14").Find("b", LookIn:=xlValues)
If Not sinepRng Is Nothing Then
firstAddress = sinepRng.Address
MsgBox "first address " & firstAddress
Do
rangeRover.Value = "yes"
Set rangeRover = rangeRover.Offset(1, 0)
Set sinepRng = Range("A1:A14").Find("b").FindNext(sinepRng)
MsgBox "third address " & sinepRng.Address
If sinepRng Is Nothing Then
MsgBox "isnothing"
GoTo DoneFinding
End If
Loop While sinepRng.Address <> firstAddress
End If
DoneFinding:
此代码确实有效。
Sub nag()
Dim rangeRover As Range
Set rangeRover = Range("B2")
With Worksheets(1).Range("a1:a500")
Set c = .Find("b", LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
rangeRover.Value = "yes"
Set rangeRover = rangeRover.Offset(1, 0)
Set c = .FindNext(c)
If c Is Nothing Then
GoTo DoneFinding
End If
Loop While c.Address <> firstAddress
End If
DoneFinding:
End With
End Sub
我认为问题在于findNext。我尝试修改它但没有成功。我还尝试使用 Find 和 After 参数。
【问题讨论】:
-
Set sinepRng = Range("A1:A14").Find("b").FindNext(sinepRng)看起来很可疑。 -
删除该行的
.Find("b")部分。 -
@Rory 谢谢,做到了!我不知道为什么我认为我需要在该行中重述 .Find。