循环

1.For Each

Sub TestHellow()

Dim r As Range, i As Integer
i = 1
For Each r In Range("A1:A100")
    r.Value = i
    i = i + 1
Next r

MsgBox "完成!"

End Sub

结果:

 

 

 

2.For 

Sub TestHellow()

Dim i, j As Integer
For i = 1 To 9
For j = 1 To 9
    If i < j Then
        Sheet1.Cells(i, j).Value = ""
    Else
        Sheet1.Cells(i, j).Value = j & "X" & i & "=" & i * j
    End If
Next j
Next i

MsgBox "完成!"

End Sub

结果:

 

 

 

3.Do While

1)条件放前

Sub TestHelloWorld()

Dim i As Integer

i = 1
Do While Sheet1.Cells(i, 1).Value <> ""
    MsgBox "A" & i & "的内容为:" & Sheet1.Cells(i, 1).Value
    i = i + 1
Loop

End Sub

结果:

 

 

 2)条件放后(至少执行一次)

Sub TestHelloWorld()

Dim i As Integer

i = 1
Do
    MsgBox "A" & i & "的内容为:" & Sheet1.Cells(i, 1).Value
    i = i + 1
Loop While Sheet1.Cells(i, 1).Value <> ""

End Sub

3)Exit Do(i = 5 跳出结束)

Sub TestHelloWorld()

Dim i As Integer

i = 1
Do
    MsgBox "A" & i & "的内容为:" & Sheet1.Cells(i, 1).Value
    If i = 5 Then
    Exit Do
    End If
    i = i + 1
Loop While Sheet1.Cells(i, 1).Value <> ""

End Sub

 

posted @ 2020-12-20 22:15  蜗牛的礼物  阅读(75)  评论(0编辑  收藏  举报