excel VBA循环语句使用教程
时间:2023-03-07 05:51:26
本文教你如何使用VBA循环语句。
1)For Next语句 以指定次数来重复执行一组语句
For counter = start To end [Step step] ‘ step 缺省值为1
[statements]
[Exit For]
[statements]
Next [counter]
如1:
For Words = 10 To 1 Step -1 ‘ 建立 10 次循环
For Chars = 0 To 9 ‘ 建立 10 次循环
MyString = MyString & Chars ‘ 将数字添加到字符串中
Next Chars ‘ Increment counter
MyString = MyString & " " ‘ 添加一个空格
Next Words
2)For Each…Next语句 主要功能是对一个数组或集合对象进行,让所有元素重复执行一次语句
For Each element In group
Statements
[Exit for]
Statements
Next [element]
如1:
For Each rang2 In range1
With range2.interior
.colorindex=6
.pattern=xlSolid
End with
Next
这上面一例中用到了 With…End With 语句,目的是省去对象多次调用,加快速度;语法为:
With object
[statements]
End With
3)Do…loop语句 在条件为true时,重复执行区块命令
Do {while |until} condition’ while 为当型循环,until为直到型循环,顾名思义,不多说啦
Statements
Exit do
Statements
Loop
或者使用下面语法
Do ‘ 先do 再判断,即不论如何先干一次再说
Statements
Exit do
Statements
Loop {while |until} condition
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10如何使用键盘关机?
![](https://img.aspxhome.com/file/2023/1/49831_0s.jpg)
wps文字怎样打印文档
Win7旗舰版玩CF不兼容的解决方法
![](https://img.aspxhome.com/file/2023/1/a280432_0s.jpg)
Win7电脑的分辨率怎么调?
![](https://img.aspxhome.com/file/2023/5/a316439_0s.png)
Win7旗舰版开机蓝屏代码0x0000007a怎么办?
![](https://img.aspxhome.com/file/2023/26/a229265_0s.jpg)
电脑版wps文档如何将带圈的页脚文字恢复原状
![](https://img.aspxhome.com/file/2023/4/a182994_0s.png)
如何在PPT中插入声音?PPT演示文稿中嵌入声音文件方法
![](https://img.aspxhome.com/file/2023/10/a346886_0s.jpg)
Win10 20H2怎么升级到22H2?Win10 20H2升级到22H2的方法
![](https://img.aspxhome.com/file/2023/25/a216760_0s.png)
Win11更新失败错误代码0x80070003解决方法
![](https://img.aspxhome.com/file/2023/1/a282039_0s.png)
Excel非常实用的数据处理操作技巧详解
![](https://img.aspxhome.com/file/2023/4/42654_0s.png)
Win10无法安装AutoCAD怎么办?Win10无法安装AutoCAD的解决方法
![](https://img.aspxhome.com/file/2023/25/a218800_0s.png)
Excel中TEXT函数日期处理应用实例教程
如何在 Mac 上录制屏幕
![](https://img.aspxhome.com/file/2023/6/a204086_0s.png)
excel删除公式的教程
word中怎样快速找到指定页码数和行数 word极速定位页码行数技巧
word 每页行数怎么设置
![](https://img.aspxhome.com/file/2023/0/33240_0s.jpg)
电脑版wps表格如何使用match函数进行查找匹配
![](https://img.aspxhome.com/file/2023/2/a183922_0s.png)
如何在Word2010中查找重复
![](https://img.aspxhome.com/file/2023/3/30053_0s.jpg)
excel中计算某个值出现的次数教程
Win10秋季创意者更新16193 PC 快速预览版已知问题大全
![](https://img.aspxhome.com/file/2023/28/a242144_0s.jpg)