For...Next statement is another type of loop but without check or condition to test. The loop is fix between numbers. Base on table below we are going to use For...Next statement to get total monthly sales.
VBA Code:
Option Explicit
Sub ForNextStatements()
Dim i As Integer
For i = 3 To 14
Range("D" & i) = Range("B" & i) + Range("C" & i)
Next i
End Sub
Sub ForNextStatements()
Dim i As Integer
For i = 3 To 14
Range("D" & i) = Range("B" & i) + Range("C" & i)
Next i
End Sub
To do in reverse way:
Option Explicit
Sub ForNextStatementsRev()
Dim i As Integer
For i = 14 To 3 Step -1
Range("D" & i) = Range("B" & i) + Range("C" & i)
Next i
End Sub
Sub ForNextStatementsRev()
Dim i As Integer
For i = 14 To 3 Step -1
Range("D" & i) = Range("B" & i) + Range("C" & i)
Next i
End Sub
Note:
After Next without mentioned i also work perfectly but prefer to add i after Next because whenever your code getting complicated then difficult to trace For..Next loop overlapping.
Microsoft Reference - For..Next statement
Practice makes perfect. Thank You.
No comments:
Post a Comment