The Visual Basic Do Until Loop


The Visual Basic Do Until Loop

The Do Until loop is very similar to the Do While loop. The loop repeatedly executes a section of code until a specified condition evaluates to True. This is shown in the following subroutine, where a Do Until loop is used to extract the values from all cells in Column A of a Worksheet, until it encounters an empty cell :
Do Until IsEmpty(Cells(iRow, 1))
    ' Store the current cell value in the dCellValues array
    dCellValues(iRow) = Cells(iRow, 1).Value
    iRow = iRow + 1
Loop


In the above example, since the condition IsEmpty(Cells(iRow, 1)) is at the start of the Do Until loop, the loop will only be entered if the first cell encountered is non-blank.
However, as illustrated in the Do While loop, you may on some occasions want to enter the loop at least once, regardless of the initial condition. In this case, the condition can be placed at the end of the loop, as follows:
Do
  .
  .
  .
Loop Until IsEmpty(Cells(iRow, 1))

Comments

Popular posts from this blog

50 Excel VBA Oral Interview Questions