如何结束循环是变量为空

How to end the loop is the variable is empty

Sub max()

Sheets(1).Select
Sheets(1).Name = "Sheet1"

Dim rng As Range
Dim celladdress As String
Dim celling As Variant

Do Until IsEmpty(celling)

    If celling > "G4" Then
    
    Set rng = Range("G3:G1000").Find(what:="Description")
    rng.Find what:="Description"
    
    celladdress = rng.Address(-1)
    celling = celladdress
    Else: Call Source

    End If
Loop

MsgBox "done"

End Sub

嗨,我正在尝试在我的范围内找到描述这个词,如果找到描述,那么它应该 运行 宏然后循环。但如果变量为空且未找到变量描述,我希望循环结束并显示消息框。我尝试使用 loop 结束循环,直到 celling 为空,但它似乎不起作用。变量 celling 引用为空,所以我不确定为什么这不起作用。非常感谢任何帮助,感谢 max

'Max,有点猜测你的意图 - 可能需要你的帮助。这会让你更亲近吗?我不认为我可以在 GNU/Linux 盒子上做得更好。

Sub max()

Sheets(1).Select
Sheets(1).Name = "Sheet1"

Dim rng As Range
Set rng = Range("G3:G1000")

Dim celladdress As String
Dim celling As Range
 
Set celling = rng.Find(what:="Description")
If celling Is Nothing Then
    MsgBox "Not found, exiting"
    Exit Sub
End If

Do 
    'Set celling = range.FindNext    'Keeps returning first range found! Maybe "With" block on rng will work.
    If celling.Row > 4 Then
        'celling.Activate
        celladdress = celling.Offset(-1, 0).Address
        MsgBox celladdress
    'Else: Call Source   'What is Source? Not this sub, is it?
    
    End If
    Set celling = range.FindNext 
Loop Until celling Is Nothing

MsgBox "done"

End Sub

Max,这值得作为新答案发布,以突出 FindNext 的不直观行为。这行得通-比上述答案更好的候选人。可能有点迂腐,因为可能有更优雅的解决方案:

Sub max()

Sheets(1).Select
Sheets(1).Name = "Sheet1"

Dim rng As Range
Set rng = Range("G3:G1000")

Dim celladdress As String
Dim celladdressPrevious As String
Dim celling As Range
 
Set celling = rng.Find(what:="Description")
If celling Is Nothing Then
    MsgBox "Not found, exiting"
    Exit Sub
End If

Do
    'Set celling = range.FindNext    'Keeps returning first range found! Maybe "With" block on rng will work.
    If celling.Row > 4 Then
        'celling.Activate
        celladdress = celling.Offset(-1, 0).Address
        If celladdress = celladdressPrevious Then GoTo WereDone
        celladdressPrevious = celladdress
        MsgBox celladdress
    'Else: Call Source   'What is Source? Not this sub, is it?
    
    End If
    If celling.Row = 1000 Then Exit Sub
    Set rng = Range("G" & celling.Row & ":G1000")
    Set celling = rng.Find(what:="Description")
Loop Until celling Is Nothing

WereDone:
MsgBox "done"

End Sub