excel - 如何结束循环是变量为空
问题描述
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
嗨我试图在我的范围内找到单词描述,如果描述是 foudn 那么它应该运行宏然后循环。但如果变量为空且未找到变量描述,我希望循环结束并显示 msgbox。我试图使用循环结束循环,直到单元格为空,但它似乎不起作用。变量 celling 引用为空,所以我不确定为什么这不起作用。任何帮助将不胜感激,感谢max
解决方案
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
推荐阅读
- c# - 处理来自对象的空 Json 数组
- reactjs - Redux 将 dispatch 连接到 props
- python-3.x - Y = churn.iloc[:, 18].values
- c# - xunit.net 测试发现并运行,但“调用执行程序 'executor://xunit/VsTestRunner2/uap' 时发生异常”
- java - Hashmap 非空元组 vavr
- tsql - 连接两个表时需要多个条件的真假答案
- delphi - 为什么CopyRect在delphi 10.3中翻转了第二张图片?
- python - 如何在 Cython 中返回 C++ 包装的对象
- java - 如何为算术计算java解析器编写递归while循环
- c# - MSTest:测试分组