msgbox不接受可选参数
我正在为这个基本的代码而苦苦挣扎:
If Dir(LocationAddress & "\" & chart & " Complete.pdf") = "" Then
MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)
Else
MsgBox ("The file was created.")
End If
当我在VBA编辑器中单击“保存”时,与错误消息相对应的行变成红色,当我尝试执行时,它告诉我有一个语法错误。我在网上找到了类似的代码,具有不同的语法,即使复制并粘贴到编辑器中也是不起作用的。
MsgBox("Important message", MsgBoxStyle.Critical, "MsgBox Example")
我还只使用一种样式说明作为可选的参数运行了我的初始代码,以确保问题不仅仅是我将它们与不当语法结合在一起。
我几乎对VBA一无所知,我只通过在网上复制和编辑代码写了一些潜艇。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如上所述,在行
msgbox上(“未创建文件)。
当行上只有一个语句时,VBA知道参数是
msgbox
过程。如果您在参数周围放括号,它将尝试先评估括号内的所有内容,从而导致错误(这就是为什么msgbox(“创建文件”)
仍然有效,即使括号再次不需要)。要么将行写入
msgbox“未创建文件”。
因此, 创建。”,vbCritical + vbretrycancel)。之所以起作用,是因为您有两个语句,
呼叫
和msgbox
,并且需要支架来指示参数属于的过程。As igittr commented above, on the line
MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)
, the parenthesis aren't needed.When there's only one statement on the line, then VBA knows that the arguments are for the
MsgBox
procedure. If you put brackets around the arguments, it will try to evaluate everything within the brackets first, resulting in the error (that's whyMsgBox ("The file was created.")
still works, even though the brackets again aren't needed).So either write the line as
MsgBox "The file wasn't created.", vbCritical + vbRetryCancel
Or, if you want to still use brackets, use
Call MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)
. This works because you have two statements,Call
andMsgBox
, and the brackets are needed to indicate what procedure the arguments belong to.由于您使用某些按钮选项显示消息,因此工作解决方案应看起来为:
Since, you show the message with some buttons option, the working solution should look as:
当您将功能称为子时,这将无法正常工作。当调用功能为subs时,它需要被视为子,这意味着没有括号。这是功能的核心基本 - 括号,没有用于潜艇的括号。
这将起作用,因为您不为子使用括号。围绕参数均值通过该参数按值的括号。如果可能的计算机语言通过引用更有效。
但是,这意味着字符串已复制,并将副本提供给MSGBox。因此,它使用了更多的内存。
呼叫
呼叫
已过时。在QuickBasic中,在调用Inline Assembler函数时,它允许使用不同的调用约定。汇编器没有返回值。因此,该约定是通过引用将变量作为输入参数传递,并且返回时将保持返回值。dir
dir
已过时。当它被文件系统对象替换时,它在90年代变得过时了。请参阅basic in
https://ericlippert.com/2003/09/15/what what what what what what You-Mean-Cannot-use-parentes/
作者是Microsoft的VBScript的维护程序员。
Trivia
msgbox
函数是WindowsMessageBox()
函数周围的包装器。逐字通行的基本传递您传递的任何参数。当然,只有在Windows上,您才能使用基本文档中未列出的系统参数。参见 https://learn.microsoft.com /en-us/windows/win32/api/hinuser/nf-winuser-messageboxThis won't work as you are calling a function as a sub. When calling functions as subs it needs to be treated as a sub which means no brackets. This is core Basic - brackets for functions and no brackets for subs.
This will work because you aren't using brackets for the sub. Brackets around a parameter mean pass that parameter by value. Where ever possible computer languages pass by reference as it more efficient.
However it means that the string is copied, and the copy given to msgbox. So it uses more memory.
Call
call
is obsolete. In QuickBasic it allowed a different calling convention when calling inline assembler functions. Assembler didn't return a value. So the convention was to pass a variable by reference as an input parameter and on return would hold the return value.Dir
Dir
is obsolete. It became obsolete in the 90s when it was replaced by the file system object. See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filesystemobject-objectBrackets in Basic
From https://ericlippert.com/2003/09/15/what-do-you-mean-cannot-use-parentheses/
The author was the maintainer programmer at Microsoft for VBScript.
Trivia
The
msgbox
function is a wrapper around the WindowsMessageBox()
functions. Basic passes on verbatim any parameters you pass. Of course only on Windows can you use the system parameters not listed in the Basic docs. See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox