Explain about For - - - Next Loops
(i)It denotes end of Loop structure. When we mentioned more than one loop for recognization purpose only we can mention "counter" key word subsequent to NEXT. Ex: Next i, Next j ....
(ii)Every for must end with NEXT
Last Cell - XL Down Method
While running the loop defining max value plays vital role. What we have know how many times loop has to run. Max value to loop defines number of times that loop has to execute
1)From top to Bottom
2)From Bottom to top
3):Loop end value based on CURRENT REGION
Private Sub CommandButton1_Click()
Dim s As Integer
s = Sheets.Count
For i = 1 To s
Sheets(i).Activate
'selection of current region from range E3., tc copy
ActiveSheet.Range("E3").CurrentRegion.Select
Selection.Copy
Next
End Sub
4)For Loop Max value = Sheets.count
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To Sheets.Count
Sheets(i).Activate
ActiveSheet.Range("a1:B11").Value = "Sriguranjani"
ActiveSheet.Range("a1:B11").EntireColumn.AutoFit
Next
End Sub
5)For Loop Max value = Number
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 101
Cells(i, 1).Value = i
Range("B" & i).Value = i
Next
End Sub
6)Loop Max\end value = SELECTION
Private Sub CommandButton1_Click()
Dim rng As Range
Set rng = selection
For i = 1 To rng.Cells.Count
Cells(i, 2) = rng.Cells(i, 1).Value
Next
End Sub
7)Loop Max\end value = INPUTBOX
Private Sub CommandButton1_Click()
Dim r As Range
Set r = Application.InputBox("select value", Type:=8)
For i = 1 To r.Cells.Count
Cells(i, 2).Value = r.Cells(i, 1).Value
Next
End Sub
7)Selection.rows.count
8)Loop Runs Till the End value
Private Sub CommandButton1_Click()
'Columns(1).Select
For i = 1 To Columns(1).Rows.Count
If Cells(i, 1) = "" Then
Exit For
ElseIf Cells(i, 1) <> "" Then
Cells(i, 2).Value = Cells(i, 1)
End If
Next
End Sub
Explain about For Each - - - Next Loop structure