【问题标题】:VBA : Exit While in If [duplicate]VBA:如果在[重复]中退出
【发布时间】:2016-05-02 21:53:05
【问题描述】:

好的,可能很简单,但我刚刚开始使用这种语言和这段代码:

While DATA.Cells(1, i).value & "" <> ""
    If InStr(DATA.Cells(1, i).value, columnName) > 0 Then
        column = i
        Exit While
    End If
    i = i + 1
Wend

看起来这不是使用 Exit While 的方式?那我该怎么做呢?

【问题讨论】:

  • 它是重复的,但问题名称很容易被其他人找到。 @duDE
  • 您在 While 语句中的条件让我感到困惑。你能试着描述一下它应该做什么吗?
  • 虽然 CELL.value 不是空字符串。

标签: vba excel loops while-loop


【解决方案1】:

While/Wend 只能通过 GOTO 或通过从外部块退出 (Exit sub/function/another exitable loop) 提前退出

改为 Do 循环

Do While DATA.Cells(1, i).value & "" <> ""
If InStr(DATA.Cells(1, i).value, columnName) > 0 Then
    column = i
    Exit Do
End If
i = i + 1
Loop

@Alex K 的原始答案。 Break out of a While...Wend loop in VBA

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 2016-07-29
    • 2021-09-27
    • 2019-04-01
    • 2021-05-05
    • 1970-01-01
    • 2016-11-12
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多