【问题标题】:How to end the loop is the variable is empty如何结束循环是变量为空
【发布时间】:2020-12-31 15:07:55
【问题描述】:
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

【问题讨论】:

    标签: excel vba vba7 vba6


    【解决方案1】:

    '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
    

    【讨论】:

    • 您好 Klausnrooster,感谢您的回复。是的,非常接近。我遇到的困难是,当它通过循环运行并将数据排序成一行时,它会留下一组数据,底部没有描述。我离开了 msgbox“完成”所以我知道当循环中断时在哪里添加代码。我已经尝试过你的方法,但它离工作太近了。
    • 我正在尝试确定单元格是否为空白/空/无,然后我可以打破循环,然后将我的附加代码添加到底部。如果您能帮我解决这个问题,将不胜感激,谢谢 max
    • 麦克斯,我冲了。对不起。我正在修复我的愚蠢错误并击中了一个关闭我唯一的 Windows PC 的组合键。现在它不会运行了。我会看看我是否可以在这个 GNU/Linux 机器上修复我的错误。等等……
    • 感谢 klausnrooster 非常感谢您的帮助
    • 我已经尝试使用 with 块,但我不确定我是否以正确的方式使用它。如果您明天有空闲时间可以帮我解决一下吗?
    【解决方案2】:

    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
    

    【讨论】:

    • 调用源是我运行的不同子程序,用于将数据移动到准备传输到我的主数据表的新行中。简化的代码基本上将数据移动到新行,然后删除范围。我认为这是我不断收到错误的地方,因为当它循环时,我希望它在删除数据后查找新的描述值。因此,例如,当我运行此代码时,它会在 G11 处完美找到它,但在理论上应该删除数据,然后循环回到起点,然后查找下一个描述,删除时将是 G12。
    • 因为范围大小不同,我需要在删除后找到下一个描述,如果找不到描述,则需要打破循环并找到下一个空白
    • 如果您对我如何实现这一目标有任何建议,非常感谢。
    • 小心删除单元格、行或列。许多其他人可能会被转移以“填补空白”,并且他们的所有地址都发生了变化。然后循环将跳过一些,除非您从最后一个单元格/行/列开始并向后工作(通常向上和/或向左)。我怀疑有一种很好的方法可以提供相对简单的预期结果,但可能意味着您必须放弃到目前为止的部分或全部代码。来自 Excel 的屏幕截图显示了开始、最终结果,也许还有一些中间状态可以真正传达目标。首先用 A / 1 替换私有文本/数字。
    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 2016-01-12
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多