VBA循环

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

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

循环语句允许多次执行语句或语句组。 以下是VBA中循环语句的一般形式。

一、循环结构

1、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)
   Next
End Sub

 

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]
Next

示例:

Private Sub Constant_demo_Click()
   'fruits is an array
   fruits = Array("Apple", "Orange", "Cherry")
   Dim fruitnames As Variant

   'iterating using For each loop.
   For Each Item In fruits
      fruitnames = fruitnames & Item & Chr(10)
   Next

   MsgBox fruitnames
End Sub

 

3、While Wend循环

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

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

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

结果:

The Current Value of the Counter is : 11 

The Current Value of the Counter is : 12 

The Current Value of the Counter is : 13 

The Current Value of the Counter is : 14 

The Current Value of the Counter is : 15

4、do...while循环

Private Sub Constant_demo_Click()
   Do While i < 5
      i = i + 1
      msgbox "The value of i is : " & i
   Loop
End Sub

结果:

The value of i is : 1

The value of i is : 2

The value of i is : 3

The value of i is : 4

The value of i is : 5

#另外一种do while 语句 ,直到while语句值为false,循环终止。

Do 
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop While condition

示例:

Private Sub Constant_demo_Click() 
   i = 10
   Do
      i = i + 1
      MsgBox "The value of i is : " & i
   Loop While i < 13 'Condition is false.Hence loop is executed once.
End Sub

 

 

 

5、do until 语句

Private Sub do_until_loop()
    Dim i As Integer
    Dim s As Integer
    s = 0
    i = 1
    
    Do Until i > 10
    s = s + i
    i = i + 1
    'MsgBox ("i值是:" & i & " S值是:" & s)
    Loop
    
    MsgBox ("S值是:" & s)

End Sub
结果:

S=55

 

 

 

 

 

 

 

二、循环控制语句

1、当想要根据特定标准退出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
   Next
End Sub

结果如下,i分别等于0,2,4,等到i=4时,i=40,然后跳出循环。

2、Exit do 语句

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

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

Private Sub Constant_demo_Click()
   i = 0
   Do While i <= 100
      If i > 10 Then
         i=i+100
         MsgBox ("The Value of i is : " & i)
         Exit Do   ' Loop Exits if i>10
      End If
      MsgBox ("The Value of i is : " & i)
      i = i + 2
   Loop
End Sub

结果如下:

 

 

 

 

 

 

 

### VBA 中的循环用法 #### For...Next 循环 `For...Next` 是一种常用的计数型循环,在已知迭代次数的情况下非常适用。以下是 `For...Next` 的基本语法: ```vba For 变量 = 初值 To 终值 [Step 步长] ' 执行代码块 Next [变量] ``` 下面是一个简单的例子,展示如何使用 `For...Next` 输出 1 到 10 的数字[^4]。 ```vba Sub Example_ForLoop() Dim i As Integer For i = 1 To 10 Debug.Print i Next i End Sub ``` --- #### Do...While 循环 `Do...While` 循环会在每次执行前先检查条件是否成立。如果条件为真,则执行循环体中的代码。其语法如下所示[^3]: ```vba Do While 条件 ' 执行代码块 Loop ``` 以下示例展示了如何利用 `Do...While` 让用户输入正整数,直到输入负数为止[^5]。 ```vba Sub Example_DoWhile() Dim num As Integer Do While num >= 0 InputBox "请输入一个数字:", , num If IsNumeric(num) Then Debug.Print num End If Loop End Sub ``` --- #### Do...Until 循环 与 `Do...While` 不同的是,`Do...Until` 在条件不满足时会一直运行循环。它的两种形式分别是顶部测试和底部测试[^1]。 ##### **顶部测试** 在循环开始之前检测条件是否满足。 ```vba Do Until 条件 ' 执行代码块 Loop ``` ##### **底部测试** 即使初始条件下不符合要求,也会至少执行一次循环体内的代码。 ```vba Do ' 执行代码块 Loop Until 条件 ``` 下面是通过 `Do...Until` 实现的一个简单案例,打印从 1 至 27 的所有自然数[^1]。 ```vba Sub Example_DoUntil_BottomTest() Dim count As Integer count = 1 Do Debug.Print count count = count + 1 Loop Until count > 27 End Sub ``` --- #### While...Wend (旧版写法) 虽然现代 VBA 更推荐使用 `Do...While` 或其他更清晰的形式,但在某些情况下仍可能遇到这种较老式的写法[^5]。 ```vba While 条件 ' 执行代码块 Wend ``` 注意:此结构已被淘汰,建议优先考虑更为标准的 `Do...While` 替代方案。 --- #### Break 和 Continue 功能模拟 尽管 VBA 并未内置像 C++ 那样的显式关键字支持 break/continue 操作,但我们可以通过标志位来实现类似功能[^5]。 例如中断特定情况下的循环流程: ```vba Sub Example_ExitDo() Dim i As Integer i = 1 Do While True If i Mod 5 = 0 And i <> 0 Then Exit Do Debug.Print i i = i + 1 Loop End Sub ``` --- ### 总结 以上分别介绍了几种常见的 VBA 循环控制语句及其具体应用场景。每种都有各自的特点以及适应范围,请根据实际需求选取合适的解决方案[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值