a印出1~3,遞增1。
Sub test()
For a = 1 To 3 Step 1
MsgBox a
Next a
End Sub
a印出1、3、5、7、9,遞增2。
Sub test()
For a = 1 To 10 Step 2
MsgBox a
Next a
End Sub
按ESC
Sub test()
For i = 1 To 12
Worksheets(1).Copy Before:=Worksheets(1)
With Worksheets(1)
.Name = i & "月"
.Range("B2").Value = Year(Date)
.Range("B3").Value = i
.Columns(1).AutoFit
End With
Next i
End Sub
Sub test()
For Each a In Range("A1:B2")
MsgBox a.Address
Next
End Sub
Sub test()
r = 1
Do While Not IsEmpty(Cells(r, 1).Value)
MsgBox Cells(r, 1).Value
r = r + 1
Loop
End Sub
與Do while剛好相反
Do while是符合條件則繼續。
Do Until是符合條件則中止。
Sub test()
r = 1
Do Until IsEmpty(Cells(r, 1).Value)
MsgBox Cells(r, 1).Value
r = r + 1
Loop
End Sub