添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

当需要多次执行一段代码时,就可以使用循环语句。 一般来说,语句是按顺序执行的:函数中的第一个语句首先执行,然后是第二个,依此类推。

编程语言提供了各种控制结构,允许更复杂的执行路径。

循环语句允许多次执行语句或语句组。 以下是VBA中循环语句的一般形式。
在这里插入图片描述
在这里插入图片描述

1.5.1 For 循环
for循环是一种重复控制结构,它允许开发人员有效地编写需要执行特定次数的循环。

For counter = start To end [Step stepcount]
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
   ....
   [statement n]

在这里插入图片描述
以下是For循环中的控制流程 -

  • For步骤先执行。这一步允许您初始化任何循环控制变量,并递增步进计数器变量。
  • 其次,评估条件。 如果评估结果为:True,则循环体被执行。 如果为False,则循环体不会执行,并且控制流将跳转到紧跟在For循环之后的下一个语句。
  • 在执行For循环体之后,控制流将跳转到下一个语句。 这个语句更新任何循环控制变量。 它根据步计数器值进行更新。
  • 现在条件再次评估。 如果条件为:True,则循环执行并且该过程重复自身(循环体,然后递增步,然后再次条件)。 条件变为False后,For循环终止。
Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10
   For i = 0 To a Step 2
      MsgBox ("The value is i is : " & i)
End Sub

1.5.2 For Each循环
For Each循环用于为数组或集合中的每个元素执行语句或一组语句。
For Each循环与For循环类似; 然而,For Each循环是为数组或组中的每个元素执行的。 因此,这种类型的循环中将不存在步计数器。 它主要用于数组或在文件系统对象的上下文中使用,以便递归操作。

For Each element In Group
   [statement 1]
   [statement 2]
   ....
   [statement n]
   [Exit For]
   [statement 11]
   [statement 22]
Private Sub Constant_demo_Click()  
   'fruits is an array
   fruits = Array("苹果", "橙子", "樱桃")
   Dim fruitnames As Variant
   'iterating using For each loop.
   For Each Item In fruits
      fruitnames = fruitnames & Item & Chr(10)
   MsgBox fruitnames
End Sub

1.5.3 While Wend循环
在While…Wend循环中,如果条件为True,则会执行所有语句,直到遇到Wend关键字。

如果条件为false,则退出循环,然后控件跳转到Wend关键字后面的下一个语句。

While condition(s)
   [statements 1]
   [statements 2]
   ...
   [statements n]
Private Sub Constant_demo_Click()
   Dim Counter :  Counter = 10   
   While Counter < 15     ' Test value of Counter.
      Counter = Counter + 1   ' Increment Counter.
      msgbox "The Current Value of the Counter is : " & Counter
   Wend   ' While loop exits if Counter Value becomes 15.
End Sub

1.5.4 Do…While循环
一个Do…while循环用于只要条件为真就重复一组语句。该条件可以在循环开始时或循环结束时检查。

Do While condition
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Private Sub Constant_demo_Click()
   Do While i < 5
      i = i + 1
      msgbox "The value of i is : " & i
End Sub

另外还有一个替代语句Do…while循环,用于在循环结束时检查条件。下面的例子解释了这两种语法的主要区别。语法 -

[statement 1] [statement 2] ... [statement n] [Exit Do] [statement 1] [statement 2] ... [statement n] Loop While condition
Private Sub Constant_demo_Click() 
   i = 10
      i = i + 1
      MsgBox "The value of i is : " & i
   Loop While i < 3 'Condition is false.Hence loop is executed once.
End Sub

1.5.5 Exit For语句
当想要根据特定标准退出For循环时,就可以使用Exit For语句。当执行Exit For时,控件会立即跳转到For循环之后的下一个语句。
在这里插入图片描述

Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10
   For i = 0 To a Step 2 'i is the counter variable and it is incremented by 2
      MsgBox ("The value is i is : " & i)
      If i = 4 Then
         i = i * 10 'This is executed only if i=4
         MsgBox ("The value is i is : " & i)
         Exit For 'Exited when i=4
      End If
End Sub

1.5.6 Exit Do语句
当想要根据特定标准退出Do循环时,可使用Exit Do语句。 它可以同时用于Do…While和Do…Until直到循环。

当Exit Do被执行时,控制器在Do循环之后立即跳转到下一个语句。

Private Sub Constant_demo_Click()
   i = 0
   Do While i <= 100
      If i > 10 Then
         Exit Do   ' Loop Exits if i>10
      End If
      MsgBox ("The Value of i is : " & i)
      i = i + 2
End Sub
                    VBA基础学习之循环语句当需要多次执行一段代码时,就可以使用循环语句。 一般来说,语句是按顺序执行的:函数中的第一个语句首先执行,然后是第二个,依此类推。编程语言提供了各种控制结构,允许更复杂的执行路径。循环语句允许多次执行语句或语句组。 以下是VBA中循环语句的一般形式。1.5.1 For 循环for循环是一种重复控制结构,它允许开发人员有效地编写需要执行特定次数的循环。Fo...
				
跳出for循环大概三种层次(1)跳出本次for循环,进行本层的下一次循环(2)跳出本层for循环,执行for语句之外的其他语句(3)跳出整个嵌套循环,执行嵌套循环以外的语句本菜写的小例子如下:Sub try() Dim i As Integer Dim j As Integer For i = 1 To 10 For j = 1 To 10 If j &lt; 2 The...
Sub ZoomColumns() ActiveWindow.Zoom = 150 '设置窗口缩放比例为150% Columns.Select '选择所有列 Selection.ColumnWidth = Selection.ColumnWidth * 1.5 '将列宽乘以1.5 End Sub 将代码复制到Excel的VBA编辑器中,然后按F5运行即可。请注意,这将会改变所有列的宽度,而不仅仅是当前工作表的列。如果你只想缩放当前工作表的列,请使用以下代码: Sub ZoomCurrentSheetColumns() ActiveWindow.Zoom = 150 '设置窗口缩放比例为150% ActiveSheet.Columns.Select '选择当前工作表的所有列 Selection.ColumnWidth = Selection.ColumnWidth * 1.5 '将列宽乘以1.5 End Sub