继续For循环

发布于 2024-11-04 22:01:43 字数 466 浏览 1 评论 0原文

我有以下代码:

For x = LBound(arr) To UBound(arr)
  
    sname = arr(x)  
    If InStr(sname, "Configuration item") Then  
        '**(here I want to go to next x in loop and not complete the code below)**  
    End If

    '// other code to copy past and do various stuff

Next x  

我以为我可以简单地使用语句 Then Next x,但这给出了“没有声明 for 语句”错误。

我可以在 If InStr(sname, "Configuration item") Then 之后放置什么以使其继续执行 x 的下一个值?

I have the following code:

For x = LBound(arr) To UBound(arr)
  
    sname = arr(x)  
    If InStr(sname, "Configuration item") Then  
        '**(here I want to go to next x in loop and not complete the code below)**  
    End If

    '// other code to copy past and do various stuff

Next x  

I thought I could simply have the statement Then Next x, but this gives a "no for statement declared" error.

What can I put after the If InStr(sname, "Configuration item") Then to make it proceed to the next value for x?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

残月升风 2024-11-11 22:01:43

您可以使用 GoTo

Do

    '... do stuff your loop will be doing

    ' skip to the end of the loop if necessary:
    If <condition-to-go-to-next-iteration> Then GoTo ContinueLoop 

    '... do other stuff if the condition is not met

ContinueLoop:
Loop

You can use a GoTo:

Do

    '... do stuff your loop will be doing

    ' skip to the end of the loop if necessary:
    If <condition-to-go-to-next-iteration> Then GoTo ContinueLoop 

    '... do other stuff if the condition is not met

ContinueLoop:
Loop
请远离我 2024-11-11 22:01:43

您正在考虑 continue 语句,例如 Java 的Python的,但是VBA没有这样的原生语句,你不能使用VBA 的 Next 就是这样。

您可以使用 GoTo 语句来实现类似您想要做的事情,但实际上,GoTo 应该保留用于替代方案是人为且不切实际的情况。

对于只有一个“继续”条件的情况,有一个非常简单、干净且可读的替代方案:

    If Not InStr(sname, "Configuration item") Then
        '// other code to copy paste and do various stuff
    End If

You're thinking of a continue statement like Java's or Python's, but VBA has no such native statement, and you can't use VBA's Next like that.

You could achieve something like what you're trying to do using a GoTo statement instead, but really, GoTo should be reserved for cases where the alternatives are contrived and impractical.

In your case with a single "continue" condition, there's a really simple, clean, and readable alternative:

    If Not InStr(sname, "Configuration item") Then
        '// other code to copy paste and do various stuff
    End If
苦行僧 2024-11-11 22:01:43
For i=1 To 10
    Do 
        'Do everything in here and

        If I_Dont_Want_Finish_This_Loop Then
            Exit Do
        End If 

        'Of course, if I do want to finish it,
        'I put more stuff here, and then...

    Loop While False 'quit after one loop
Next i
For i=1 To 10
    Do 
        'Do everything in here and

        If I_Dont_Want_Finish_This_Loop Then
            Exit Do
        End If 

        'Of course, if I do want to finish it,
        'I put more stuff here, and then...

    Loop While False 'quit after one loop
Next i
陌伤ぢ 2024-11-11 22:01:43

很多年后......我喜欢这个:

For x = LBound(arr) To UBound(arr): Do

    sname = arr(x)  
    If instr(sname, "Configuration item") Then Exit Do 

    '// other code to copy past and do various stuff

Loop While False: Next x

A lot of years after... I like this one:

For x = LBound(arr) To UBound(arr): Do

    sname = arr(x)  
    If instr(sname, "Configuration item") Then Exit Do 

    '// other code to copy past and do various stuff

Loop While False: Next x
北凤男飞 2024-11-11 22:01:43

晚了几年,但这里有另一种选择。

For x = LBound(arr) To UBound(arr)
    sname = arr(x)  
    If InStr(sname, "Configuration item") Then  
        'Do nothing here, which automatically go to the next iteration
    Else
        'Code to perform the required action
    End If
Next x

A few years late, but here is another alternative.

For x = LBound(arr) To UBound(arr)
    sname = arr(x)  
    If InStr(sname, "Configuration item") Then  
        'Do nothing here, which automatically go to the next iteration
    Else
        'Code to perform the required action
    End If
Next x
錯遇了你 2024-11-11 22:01:43

许多年后:DI 使用“select”语句作为一个简单的示例:

  For Each zThisRow In zRowRange
    zRowNum = zThisRow.Row
    Select Case zRowNum
      Case 1 '- Skip header row and any other rows to skip -----
             '- no need to put anything here -----

      Case Else '- Rows to process -----
             '- Process for stuff to do something here -----
    End Select
  Next zThisRow

您可以通过将每个“if”结果转换为一个值来使其变得像您希望的那样复杂(也许一些过于复杂的代码将有助于解释:D) :

zSkip = 0
If 'condition1 = skip' Then zSkip = zSkip + 1
If 'condition2 = skip' Then zSkip = zSkip + 1
If 'condition3 = skip' Then zSkip = zSkip + 1
Select Case zRowNum
  Case 0 '- Stuff to do -----
  Case Else '- Stuff to skip -----
End Select

这只是一个建议;祝圣诞快乐!

And many years later :D I used a "select" statement for a simple example:

  For Each zThisRow In zRowRange
    zRowNum = zThisRow.Row
    Select Case zRowNum
      Case 1 '- Skip header row and any other rows to skip -----
             '- no need to put anything here -----

      Case Else '- Rows to process -----
             '- Process for stuff to do something here -----
    End Select
  Next zThisRow

You can make this as complex as you wish by turning each "if" result into a value (maybe a bit of over complex code would help explain :D ):

zSkip = 0
If 'condition1 = skip' Then zSkip = zSkip + 1
If 'condition2 = skip' Then zSkip = zSkip + 1
If 'condition3 = skip' Then zSkip = zSkip + 1
Select Case zRowNum
  Case 0 '- Stuff to do -----
  Case Else '- Stuff to skip -----
End Select

It's just a suggestion; have a great Christmas peeps!

青衫负雪 2024-11-11 22:01:43

这也可以使用布尔值来解决。

For Each rngCol In rngAll.Columns
    doCol = False '<==== Resets to False at top of each column
    For Each cell In Selection
        If cell.row = 1 Then
            If thisColumnShouldBeProcessed Then doCol = True
        End If
        If doCol Then
            'Do what you want to do to each cell in this column
        End If
    Next cell
Next rngCol

例如,以下是完整示例:
(1) 识别工作表上使用的单元格范围
(2) 循环遍历每一列
(3) IF 列标题是可接受的标题,循环遍历列中的所有单元格

Sub HowToSkipForLoopIfConditionNotMet()
    Dim rngCol, rngAll, cell As Range, cnt As Long, doCol, cellValType As Boolean
    Set rngAll = Range("A1").CurrentRegion
    'MsgBox R.Address(0, 0), , "All data"
    cnt = 0
    For Each rngCol In rngAll.Columns
        rngCol.Select
        doCol = False
        For Each cell In Selection
            If cell.row = 1 Then
                If cell.Value = "AnAllowedColumnTitle" Then doCol = True
            End If
            If doCol Then '<============== THIS LINE ==========
                cnt = cnt + 1
                Debug.Print ("[" & cell.Value & "]" & " / " & cell.Address & " / " & cell.Column & " / " & cell.row)
                If cnt > 5 Then End '<=== NOT NEEDED. Just prevents too much demo output.
            End If
        Next cell
    Next rngCol
End Sub

注意:如果您没有立即抓住它,则 If docol Then 行是您的倒置 CONTINUE。也就是说,如果 doCol 保持为 False,则脚本将继续到下一个单元格并且不执行任何操作。

当然不如正确的 continuenext for 语句那么快/高效,但最终结果是我所能得到的最接近的结果。

This can also be solved using a boolean.

For Each rngCol In rngAll.Columns
    doCol = False '<==== Resets to False at top of each column
    For Each cell In Selection
        If cell.row = 1 Then
            If thisColumnShouldBeProcessed Then doCol = True
        End If
        If doCol Then
            'Do what you want to do to each cell in this column
        End If
    Next cell
Next rngCol

For example, here is the full example that:
(1) Identifies range of used cells on worksheet
(2) Loops through each column
(3) IF column title is an accepted title, Loops through all cells in the column

Sub HowToSkipForLoopIfConditionNotMet()
    Dim rngCol, rngAll, cell As Range, cnt As Long, doCol, cellValType As Boolean
    Set rngAll = Range("A1").CurrentRegion
    'MsgBox R.Address(0, 0), , "All data"
    cnt = 0
    For Each rngCol In rngAll.Columns
        rngCol.Select
        doCol = False
        For Each cell In Selection
            If cell.row = 1 Then
                If cell.Value = "AnAllowedColumnTitle" Then doCol = True
            End If
            If doCol Then '<============== THIS LINE ==========
                cnt = cnt + 1
                Debug.Print ("[" & cell.Value & "]" & " / " & cell.Address & " / " & cell.Column & " / " & cell.row)
                If cnt > 5 Then End '<=== NOT NEEDED. Just prevents too much demo output.
            End If
        Next cell
    Next rngCol
End Sub

Note: If you didn't immediately catch it, the line If docol Then is your inverted CONTINUE. That is, if doCol remains False, the script CONTINUES to the next cell and doesn't do anything.

Certainly not as fast/efficient as a proper continue or next for statement, but the end result is as close as I've been able to get.

才能让你更想念 2024-11-11 22:01:43

您可以通过简单的方法来做到这一点,只需将 for 循环中使用的变量值更改为最终值,如示例所示

Sub TEST_ONLY()
    For i = 1 To 10
        ActiveSheet.Cells(i, 1).Value = i
        If i = 5 Then
            i = 10
        End If
    Next i
End Sub

you can do that by simple way, simply change the variable value that used in for loop to the end value as shown in example

Sub TEST_ONLY()
    For i = 1 To 10
        ActiveSheet.Cells(i, 1).Value = i
        If i = 5 Then
            i = 10
        End If
    Next i
End Sub
怪我鬧 2024-11-11 22:01:43

抱歉迟到了,但在我看来,使用 GOTO 是最易读、最明显的事情。您编写的代码可能需要由经验比您少得多的人来维护。哎呀,我曾经写过如此多的 VBA 代码(在 CS 和 IT 还很流行之前),我什至无法意识到我是其中一些代码的编写者。不过,感谢 25 年后的智力锻炼,谢谢!

Sorry for being late to the party, but using a GOTO seems to me to be the most readable and obvious thing to do. The code you write may need to be maintained by someone with far less experience than yourselves. Heck, I wrote so much VBA code at one time (before CS and IT were a thang) that I couldn't even recognise that I was the one who wrote some of it. Appreciate the intellectual exercise 25 years later, though, thanks!

夜深人未静 2024-11-11 22:01:43

我有时会做一个双 do 循环:

Do

    Do

        If I_Don't_Want_to_Finish_This_Loop Then Exit Do

        Exit Do

    Loop

Loop Until Done

这可以避免出现“goto spaghetti”

I sometimes do a double do loop:

Do

    Do

        If I_Don't_Want_to_Finish_This_Loop Then Exit Do

        Exit Do

    Loop

Loop Until Done

This avoids having "goto spaghetti"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文