VBA:单行if语句具有多个操作 [英] VBA: Single line if statement with multiple actions

查看:273
本文介绍了VBA:单行if语句具有多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实应该可以在Google上搜索到它,但是我找不到我想知道的东西.

I really should be able to google this, but I can't find what I wanna know about.

我要检查文件是否存在.如果不是,则应弹出一个MessageBox,VBA应该退出该子程序.

I want to check if a file exists. If not, a MessageBox should pop up and VBA should exit the sub.

If Dir("C:\file.txt", vbDirectory) = "" Then 
    MsgBox "File doesn't exist"
    Exit Sub
End If

它有效,我只想知道您是否可以在一行语句中执行此操作?如果应该发生一件以上的事情(如此处的情况),VBA是否允许这样做?此代码不起作用(语法错误):

It works, I just wanna know if you can you do this in a single line statement? Does VBA allow this when more than one thing is supposed to happen (like it is the case here)? This code doesn't work (syntax error):

If Dir("C:\file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" And Exit Sub

推荐答案

您绝对可以!

If Dir("C:\file.txt", vbDirectory) = "" Then  MsgBox "File doesn't exist" : Exit Sub

这篇关于VBA:单行if语句具有多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆