msgbox不接受可选参数

发布于 2025-02-10 07:23:19 字数 592 浏览 2 评论 0 原文

我正在为这个基本的代码而苦苦挣扎:

     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一无所知,我只通过在网上复制和编辑代码写了一些潜艇。

I'm struggling with this basic piece of code :

     If Dir(LocationAddress & "\" & chart & " Complete.pdf") = "" Then
                MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)
            Else
                MsgBox ("The file was created.")
            End If

When I click "save" in the VBA editor, the line corresponding to the error message turns red, and when I try to execute, it tells me there's a syntax error. I found this similar code online, with a different syntax, which also doesn't work, even when copied and pasted into the editor.

MsgBox("Important message", MsgBoxStyle.Critical, "MsgBox Example")

I also ran my initial code with only one style instruction as an optional argument, to make sure the issue wasn't simply that I was combining them with improper syntax.

I hardly know anything about vba, I've only written a few subs by copying and editing code found online.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2025-02-17 07:23:19

如上所述,在行 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 why MsgBox ("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 and MsgBox, and the brackets are needed to indicate what procedure the arguments belong to.

舟遥客 2025-02-17 07:23:19

由于您使用某些按钮选项显示消息,因此工作解决方案应看起来为:

    Dim ans As VbMsgBoxResult
    ans = MsgBox("The file wasn't created.", vbCritical + vbRetryCancel, "A choice...")
      If ans <> vbRetry Then Exit Sub ' the code will stop even if the right corner 'x'  will be clicked.
      'your code if want it continuing...
      'or viceversa according to what do you intend doing in case of Cancel option.

Since, you show the message with some buttons option, the working solution should look as:

    Dim ans As VbMsgBoxResult
    ans = MsgBox("The file wasn't created.", vbCritical + vbRetryCancel, "A choice...")
      If ans <> vbRetry Then Exit Sub ' the code will stop even if the right corner 'x'  will be clicked.
      'your code if want it continuing...
      'or viceversa according to what do you intend doing in case of Cancel option.
_蜘蛛 2025-02-17 07:23:19
           MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)

当您将功能称为子时,这将无法正常工作。当调用功能为subs时,它需要被视为子,这意味着没有括号。这是功能的核心基本 - 括号,没有用于潜艇的括号。

            MsgBox ("The file was created.")

这将起作用,因为您不为子使用括号。围绕参数均值通过该参数按值的括号。如果可能的计算机语言通过引用更有效。

但是,这意味着字符串已复制,并将副本提供给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的维护程序员。

这是交易:括号意味着VB和
因此在vbscript中。他们的意思是:

  1. 定义子表达的边界:平均=(第一个 + last) / 2 < / p>

  2. 取消数组的索引:item = myArray(index)

  3. 调用函数或子例程:limit = ubound(myarray)

  4. 通过一个通常被byref作为byval的参数:结果
    = myfunction(arg1,(arg2)),arg1通过参考传递,arg2is按值传递。

那已经很困惑了。不幸的是,VB以及VBScript
有一些关于#3应用何时使用的奇怪规则。规则为

3.1)一个函数调用的参数列表,其中归还值的分配必须由帕伦斯包围:result = myfunc(myarg)

3.2)subroutine调用的参数列表(或使用呼叫关键字的函数调用)必须被包围
Parens:致电MySub(Myarg)

3.3)如果不适用3.1和3.2,则不得将列表包围在Parens中。

最后有BYREF规则:参数通过参考传递
如果可能
该变量按值传递,而不是通过参考。

Trivia

msgbox 函数是Windows MessageBox()函数周围的包装器。逐字通行的基本传递您传递的任何参数。当然,只有在Windows上,您才能使用基本文档中未列出的系统参数。参见 https://learn.microsoft.com /en-us/windows/win32/api/hinuser/nf-winuser-messagebox

           MsgBox("The file wasn't created.", vbCritical + vbRetryCancel)

This 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.

            MsgBox ("The file was created.")

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-object

Brackets 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.

Here’s the deal: parentheses mean several different things in VB and
hence in VBScript. They mean:

  1. Define boundaries of a subexpression: Average = (First + Last) / 2

  2. Dereference the index of an array: Item = MyArray(Index)

  3. Call a function or subroutine: Limit = UBound(MyArray)

  4. Pass an argument which would normally be byref as byval: in Result
    = MyFunction(Arg1, (Arg2)) , Arg1 is passed by reference, Arg2is passed by value.

That’s confusing enough already. Unfortunately, VB and hence VBScript
has some weird rules about when #3 applies. The rules are

3.1) An argument list for a function call with an assignment to the returned value must be surrounded by parens: Result = MyFunc(MyArg)

3.2) An argument list for a subroutine call (or a function call with no assignment) that uses the Call keyword must be surrounded by
parens: Call MySub(MyArg)

3.3) If 3.1 and 3.2 do not apply then the list must not be surrounded by parens.

And finally there is the byref rule: arguments are passed by reference
when possible but if there are “extra” parens around a variable then
the variable is passed by value, not by reference.

Trivia

The msgbox function is a wrapper around the Windows MessageBox() 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文