Use GoTo to do a loop : GoTo « Language Basics « VBA / Excel / Access / Word






Use GoTo to do a loop

 
Sub BadLoop()
    Dim StartVal As Long
    Dim NumToFill As Long
    Dim CellCount As Long
    StartVal = InputBox("Enter the starting value: ")
    NumToFill = InputBox("How many cells? ")
    ActiveCell = StartVal
    CellCount = 1
DoAnother:
    ActiveCell.Offset(CellCount, 0) = StartVal + CellCount
    CellCount = CellCount + 1
    If CellCount < NumToFill Then GoTo DoAnother _
       Else Exit Sub
End Sub

 








Related examples in the same category

1.A line number is simply a number placed at the beginning of a line to identify it. For example, consider this demonstration of GoTo:
2.A GoTo statement with a condition
3.how a GoTo statement works: