excel vba if判断语句的使用方法,在最后以一个实例来剖析vba if语句的具体应用
时间:2022-11-12 09:30:38
vba if语句为判断语句。根据条件的值,可使用 If...Then...Else 语句运行指定的语句或一个语句块。If...Then...Else 语句可根据需要嵌套多级。
但是,为了代码的可读性,建议使用其余的语句,比如 Select Case 语句而不使用多层嵌套的vba中的 If...Then...Else 语句。
vba if语句用法解释:
当条件为 True 时,若只要执行一个语句,则可以使用单行的 If...Then...Else 语法。
使用 If...Then...Else 语句可以定义两个可执行的语句块:其中一个块会在条件为True 时执行;而另一个块会在条件为 False 时执行。
vba if语句执行流程如下:
vba if语句实例剖析
vba if语句练习题以论坛的练习题《VBA IF语句答题练习》来加以解释。
此题共两段代码:第一段:
Sub 随机数()
[a1].Value = Int(Rnd * 20)
[c1].Value = Int(Rnd * 20)
End Sub
解释:A1、C1单元格的值为随时生成的20以内的整数。Rnd是VBA中的随机函数,也可以使用excel工作表中的公式来取代上一段代码,如下:
Sub 随机数()
Range("A1,c1").Formula = "=Int(Rand() * 20)"
End Sub
第二段:
Sub 答题()
If [e1].Value = "" Then
MsgBox "还没有输入答案"
Else
End If
If [e1].Value = [a1].Value + [c1].Value Then
MsgBox "答对了,你真棒!"
Else
MsgBox "答错了,继续努力!"
End If
Call 随机数
End Sub
代码解释:第一个IF语句:如果E1单元格为空,提示消息框:还没有输入答案。
第二个IF语句:如果E1的值等于A1+C1,提示消息框:答对了,你真棒!,否则返回答错了,继续努力。最后用call关键字调用第一段代码。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
win10最新预览版 斯巴达浏览器如何禁用Flash?
![](https://img.aspxhome.com/file/2023/28/a251109_0s.jpg)
win10麦克风启用了但是没声音怎么办?
![](https://img.aspxhome.com/file/2023/30/a268900_0s.jpg)
WPS打卡领会员活动题目及答案2020大全
![](https://img.aspxhome.com/file/2023/8/a185618_0s.png)
Win10如何卸载自带输入法?
![](https://img.aspxhome.com/file/2023/26/a221897_0s.jpg)
ppt文件中怎么画半个圆形?
![](https://img.aspxhome.com/file/2023/10/a347193_0s.jpg)
解决Win11 22449 bug的几种方法介绍
![](https://img.aspxhome.com/file/2023/27/a237844_0s.jpg)
手机搜狗输入法打字卡的解决办法
Win10如何录制电脑内部声音?Windows10电脑自身录音的方法
![](https://img.aspxhome.com/file/2023/8/a336299_0s.jpg)
excel 如何删除指定数据
![](https://img.aspxhome.com/file/2023/1/a154231_0s.png)
Win10系统屏幕颜色怎么矫正?
![](https://img.aspxhome.com/file/2023/25/a218742_0s.png)
Win8的IE浏览器如何打开下载项有快捷键吗
![](https://img.aspxhome.com/file/2023/29/a251521_0s.jpg)
Excel转换成PDF格式的方法
![](https://img.aspxhome.com/file/2023/7/a153627_0s.jpg)
全民k歌抢麦积分有用吗?全民k歌抢麦积分的用法介绍
![](https://img.aspxhome.com/file/2023/8/a333845_0s.jpg)
WPS文档办公—提高单元格利用率 缩小单元格边距
Win7创建虚拟硬盘隐藏私密文件全程图解
![](https://img.aspxhome.com/file/2023/29/a252179_0s.jpg)
Win10系统用cipher命令行彻底删除硬盘文件的方法
![](https://img.aspxhome.com/file/2023/4/a312555_0s.jpg)
win102004gpu加速怎么开启_win102004gpu加速开启方法
![](https://img.aspxhome.com/file/2023/2/a286850_0s.jpg)
u盘在电脑上读不出来如何解决
![](https://img.aspxhome.com/file/2023/2/a289916_0s.png)
mhdd检测不到硬盘什么原因?mhdd检测不到硬盘如何解决?
![](https://img.aspxhome.com/file/2023/30/a267655_0s.jpg)
excel中的绝对引用的使用方法,并给出一个相关实例分析excel中的绝对引用、相对引用、混合引用
![](https://img.aspxhome.com/file/2023/5/a162765_0s.gif)