Excel VBA 编程基础 — 语句(十)- 循环语句(三)
Do Until condition[ statements ][ Exit Do ][ statements ]Loop
-
Do While condition … Loop:如果 condition 为 True 则一直循环 -
Do Until … Loop:一直循环直到 condition 为 True
Dim total As IntegerDim i As Integertotal = 0i = 0Do Until i >= 10total = total + ii = i + 1LoopDebug.Print total
-
total = total + i:表示对 i 累计求和 -
i = i + 1:递增循环控制变量,从而循环能够终止
Dim total As IntegerDim i As Integertotal = 0For i = 0 To 9total = total + 1Next iDebug.Print total
robj.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
Dim rowAsIntegerDim col AsIntegerDim cell AsRangeDim v As Variantv = ""奉止Set cell = NothingForrow=1To100For col =1to100If Cells(row, col).Value= v ThenSet cell = Cells(row, col)Exit For' 找到了,退出当前的 col 循环End IfNext colIf Not (cell Is Nothing) ThenExit For ' 已找到,退出 row 循环End IfNext rowIf cell Is Nothing ThenMsgBox "未找到单元格"End If
Dim rowAsIntegerDim col AsIntegerDim cell AsRangeDim v As Variantv = "奉止"Set cell = Nothingrow=1Do While (row<=100) And (cell Is Nothing)col =1Do While (col <=100) And (cell Is Nothing)If Cells(row, col).Value= v ThenSet cell = Cells(row, col) ' 已找到Elsecol = col + 1 ' 未找到,继续下一列End IfLooprow=row+1LoopIf cell Is Nothing ThenMsgBox "未找到单元格"End If
Dim rowAsIntegerDim col AsIntegerDim cell AsRangeDim v As Variantv = "奉止"Set cell = Nothingrow=1Do Until (row>100) OrNot (cell Is Nothing)col =1Do Until (col >100) OrNot (cell Is Nothing)if Cells(row, col).Value= v ThenSet cell = Cells(row, col)Elsecol = col +1End IfLooprow=row+1LoopIf cell Is Nothing thenMsgBox "未找到单元格"End If
-
W_condition:(row <= 100) And (cell Is Nothing) -
U_condition:(row > 100) Or Not (cell Is Nothing)
DoWhile W_condition ' 如果 W_condition 为真则一直循环...Loop
DoUntil U_condition ' 一直循环直到 U_condition 为真...Loop
-
W_condition = Not U_condition -
U_condition = Not W_condition
夜雨聆风