8

I'm trying to skip to the next iteration in my vba code, with the 'continue' statement. Its not working..

  Do Until IsEmpty(xlSheet.Cells(row, 1))
       row = row + 1
       If xlSheet.Cells(row, 2) = "Epic" Then
          Continue
       End If
       xlSheet.Cells(row, 1).Value = 5
  Loop

Any idea?? Thanks..

4 Answers 4

8

VBA does not have a Continue statement. You can get around it by doing something like

Do Until IsEmpty(xlSheet.Cells(row + 1, 1))
   row = row + 1
   If xlSheet.Cells(row, 2) <> "Epic" Then
       xlSheet.Cells(row, 1).Value = 5
   End If
Loop

or

Do Until IsEmpty(xlSheet.Cells(row + 1, 1))
    row = row + 1
    If xlSheet.Cells(row, 2) = "Epic" Then
        GoTo ContinueDo
    End If
    xlSheet.Cells(row, 1).Value = 5
ContinueDo:
Loop

Note: In both versions I changed IsEmpty(xlSheet.Cells(row, 1)) to IsEmpty(xlSheet.Cells(row + 1, 1)) or else you will end up with an infinite loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, my bad. I posted an answer based on VB keyword. Label is a nice alternative.
@pav - Yeah, I've done that on several occasions - googled something using "VBA" as part of the search terms, found a reference in MSDN, read it, posted an answer, and not realised that the MSDN page had nothing to do with VBA at all.
1

So this is another possibility that works for me. Just skip the unwanted lines...

Do Until IsEmpty(xlSheet.Cells(row, 1))
   row = row + 1
   Do while xlSheet.Cells(row, 2) = "Epic" 
      row = row + 1
   Loop
   xlSheet.Cells(row, 1).Value = 5
Loop

Comments

0

While Visual Basic has 3 continue statements for 3 different loops (Continue While, Continue Do, and Continue For), oddly Microsoft VBA does not use any of them. You are going to have to restructure your loops and conditionals to go without.

Comments

0

You can make something like this due to vba have not any continue statement using the Goto statement use for errors.

Do Until IsEmpty(xlSheet.Cells(row, 1))
       row = row + 1
       If xlSheet.Cells(row, 2) = "Epic" Then
          GoTo Exitloop
       End If
       xlSheet.Cells(row, 1).Value = 5
ExitLoop:
  Loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.