当前位置:网站首页>Excel-VBA 快速上手(二、条件判断和循环)
Excel-VBA 快速上手(二、条件判断和循环)
2022-06-26 14:06:00 【三叔笔记】
文章目录
一、条件判断
1.1. IF 判断
语法
IF 判断的语法和其他编程语言大同小异
If 条件判断 Then
'条件判断成立时的逻辑
ElseIf 条件判断 Then
'条件判断成立时的逻辑
Else
'条件都不满足时的逻辑
End If
示例
代码:
'函数内对参数进行条件判断
Function judge(num)
If num Mod 2 = 0 Then
Debug.Print "参数是偶数"
ElseIf num Mod 2 <> 0 Then
Debug.Print "参数是奇数"
Else
Debug.Print "非法数字"
End If
End Function
'调用函数
Sub main()
judge 2
End Sub
运行效果:
1.2. Select Case 判断
普通语法
Select Case '要判断的变量、也可使用表达式,相当于对表达式结果的值进行判断
Case Is '大于、小于、等于、大于等于、小于等于、不等于判断
'满足条件的逻辑
Case Is '大于、小于、等于、大于等于、小于等于、不等于判断
'满足条件的逻辑
Case Else
'条件都不满足时的逻辑
End Select
普通语法示例
代码:
'函数内对参数进行条件判断
Function judge(num)
Select Case num
Case Is > 0
Debug.Print "参数是正数"
Case Is < 0
Debug.Print "参数是负数"
Case Else
Debug.Print "参数是0"
End Select
End Function
'调用函数
Sub main()
judge 0
End Sub
运行效果:

数值区间语法
Select Case '要判断的变量、也可使用表达式,相当于对表达式结果的值进行判断
Case 区间起始数值 To 区间结束数值
'满足条件的逻辑
Case Else
'条件都不满足时的逻辑
End Select
数值区间语法示例
代码:
'函数内对参数进行条件判断
Function judge(num)
Select Case num
Case 0 To 100
Debug.Print "参数区间是0-100"
Case 101 To 200
Debug.Print "参数区间是100-200"
Case Else
Debug.Print "参数是其他区间"
End Select
End Function
'调用函数
Sub main()
judge 100
End Sub
运行效果:
1.3. IIF 判断
IIF判断与其他编程语言中的三目运算差不多,适用于按条件赋值的场景
语法
IIf(条件判断, 满足条件时的返回值, 不满足条件时的返回值)
示例
代码:
'函数内对参数进行条件判断
Function judge(num)
Debug.Print IIf(num Mod 2 = 0, "参数是偶数", "参数是奇数")
End Function
运行效果:

二、循环
2.1. For 循环
For 循环可以按照次数进行循环,也可以对数组内容进行遍历
2.1.1. 按次数循环
语法
For 变量名 = 起始数值 To 终点数值 Step 步长
循环体
Next 变量名
示例
代码:
Sub main()
For num = 1 To 20 Step 2
Debug.Print num
Next num
End Sub
运行效果:

2.1.2. 数组遍历
语法
For Each 数组元素临时变量 In 数组
循环体
Next 数组元素临时变量
示例
代码:
Sub main()
Dim items
items = Array(1, 2, 3, 4, 5, 6)
For Each Item In items
Debug.Print Item
Next Item
End Sub
运行效果:
2.2. Do 循环
Do Loop Until 是满足条件后中止循环,Do While Loop 是满足条件后开始循环,Do Loop Until 不管条件是否满足,
都会先执行一次循环体内容,也就是说 Do Loop Until 最少会循环一次
2.2.1 Do Loop Until
语法
Do
循环体
Loop Until 中止条件
示例
代码:
Sub main()
Dim count As Integer
count = 1
Do
count = count + 1
Debug.Print count
Loop Until count = 2
End Sub
运行效果:
2.2.2 Do While Loop
语法
Do While 循环条件
循环体
Loop
示例
代码:
Sub main()
Dim count As Integer
count = 1
Do While count < 10
Debug.Print count
count = count + 1
Loop
End Sub
运行效果:
2.3 提前中止循环
可以使用 End 关键字来提前中止循环,常用的场景是在集合中查找某个元素,当元素找到后就没必要继续循环下去
示例
代码:
Sub main()
Dim count As Integer
count = 1
Do While count < 10
Debug.Print count
count = count + 1
If count = 3 Then
End
End If
Loop
End Sub
运行效果:
边栏推荐
- Leaflet loading ArcGIS for server map layers
- Atcoder bit operation & Conclusion + formula derivation
- Go变量的声明与赋值
- Common controls and custom controls
- Flex & Bison 开始
- Attention meets Geometry:几何引导的时空注意一致性自监督单目深度估计
- 这才是优美的文件系统挂载方式,亲测有效
- 大学生值得珍藏的实用网站推荐
- When drawing with origin, capital letter C will appear in the upper left corner of the chart. The removal method is as follows:
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
猜你喜欢

在云服务器中云磁盘如何挂载

Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient

印尼投资部长:鸿海考虑在其新首都建立电动公交系统、城市物联网

登录认证服务

One article of the quantification framework backtrader read observer

备战数学建模32-相关性分析2

Unity 利用Skybox Panoramic着色器制作全景图预览有条缝隙问题解决办法
![[cloud native] codeless IVX editor programmable by](/img/10/7c56e46df69be6be522a477b00ec05.png)
[cloud native] codeless IVX editor programmable by "everyone"

Complimentary Book Cognitive Control: how does our brain accomplish tasks?

VMware partial settings
随机推荐
Eigen(3):error: ‘Eigen’ has not been declared
oracle11g数据库导入导出方法教程[通俗易懂]
MySQL主从复制与读写分离
Atcoder bit operation & Conclusion + formula derivation
A remove the underline from the label
Flex & bison start
ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
大学生值得珍藏的实用网站推荐
Electron
fileinput.js php,fileinput
Sword finger offer 21.57.58 I Double pointer (simple)
[solo π] ADB connects multiple mobile phones
国信证券的排名如何?办理股票开户安全吗?
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)
D - Face Produces Unhappiness
通俗语言说BM3D
方程推导:二阶有源带通滤波器设计!(下载:教程+原理图+视频+代码)
ArcGIS secondary development -- arcpy batch automatic map publishing service
PostGIS create spatial database