调用具有多个参数的 Sub 时 VBA 返回错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13624279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:29:59  来源:igfitidea点击:

VBA returning error when calling a Sub with multiple parameters

excelvbaexcel-vba

提问by MartinUKPL

I'm trying to figure out why VBAis returning an error (Compile error: Expected: =)when I call a Suband supply it with multiple parameters.

我试图弄清楚为什么在调用 a并为其提供多个参数时VBA返回错误。(Compile error: Expected: =)Sub

Sub customerController(cleanStructure As Boolean, firstCol As Integer, latCol As Integer, _
                    lngCol As Integer, Optional startRow As Long, Optional endRow As Long)

Dim i As Long, j As Long, n As Long

If (cleanStructure = False) Then
    'customer data type
    If (startRow = "") Then i = 1
    If (endRow = "") Then j = countRows
    For n = i To j - i + 1
        generateURL(n, firstCol)
        newReadXMLData (url)
        ActiveSheet.Cells(i, latCol).Value = lat
        ActiveSheet.Cells(i, lngCol).Value = lng
    Next
End If

End Sub

The Subthat I'm calling requires two parameters:

Sub这我打电话需要两个参数:

Sub generateURL(row As Long, column As Long)

回答by brettdj

When calling more than 1 parameter (i.e. just generateURL(n)works) you need to either use

当调用超过 1 个参数(即generateURL(n)正常工作)时,您需要使用

  • Call generateURL(n, firstCol), or
  • generateURL n, firstCol
  • Call generateURL(n, firstCol), 或者
  • generateURL n, firstCol

using Callis the better programming technique as it is clearer

usingCall是更好的编程技术,因为它更清晰

As per MSDN:

根据 MSDN:

You normally use the Call statement to call a procedure that does not return a value. If the procedure returns a value, the Call statement discards it. You are not required to use the Call statement when calling a procedure. However, it improves the readability of your code.

您通常使用 Call 语句来调用不返回值的过程。如果过程返回一个值,Call 语句将丢弃它。调用过程时不需要使用 Call 语句。但是,它提高了代码的可读性