Excel VBA GOTO Jump or Branch Statement
Sub square_Range()
Dim myWS As Worksheet
Set myWS = ThisWorkbook.ActiveSheet
i = 2 ' counter
Beginning: ' flag for the start of the GOTO loop
i = i + 1
Value = myWS.Range(Cells(3, i), Cells(3, i)).Value
If Value = "" Or Value = "end" Or i > 100 Or IsNumeric(Value) = False Then GoTo Out
' this statement test if the value is a numerical value, but also makes sure that we get out of the loop
' when i is bigger than 100. This is a safety you can implement or not in a loop if you wish.
square = Value * Value
myWS.Range(Cells(5, i), Cells(5, i)).Value = square
GoTo Beginning ' we go back to the beginning of the loop
Out: ' this is the exit road of the GOTO Loop statement
i = i - 3 ' setting i to the real value as we started in the second column
Cells(7, 3).Value = "we have squared all the " & i & " values in your table"
End Sub
Dim myWS As Worksheet
Set myWS = ThisWorkbook.ActiveSheet
i = 2 ' counter
Beginning: ' flag for the start of the GOTO loop
i = i + 1
Value = myWS.Range(Cells(3, i), Cells(3, i)).Value
If Value = "" Or Value = "end" Or i > 100 Or IsNumeric(Value) = False Then GoTo Out
' this statement test if the value is a numerical value, but also makes sure that we get out of the loop
' when i is bigger than 100. This is a safety you can implement or not in a loop if you wish.
square = Value * Value
myWS.Range(Cells(5, i), Cells(5, i)).Value = square
GoTo Beginning ' we go back to the beginning of the loop
Out: ' this is the exit road of the GOTO Loop statement
i = i - 3 ' setting i to the real value as we started in the second column
Cells(7, 3).Value = "we have squared all the " & i & " values in your table"
End Sub