Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - how to skip to next iteration in a Do Until...Loop

Tags:

excel

vba

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..

like image 366
Tzvibe Avatar asked Oct 05 '16 07:10

Tzvibe


2 Answers

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.

like image 161
YowE3K Avatar answered Oct 17 '22 23:10

YowE3K


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
like image 1
Rob Duikersloot Avatar answered Oct 18 '22 00:10

Rob Duikersloot