多参数替换 VBA

24

我使用Access 2010和VBA编写了一个子程序:

Public Sub setInterest(account As String, dmonth As Integer)
    ...somecode...
End Sub

我正在使用以下方式调用它:

setInterest("myAccount",3)

我遇到了语法错误。
将这个子程序修改为只接受一个参数且忽略掉第三个参数时不会出现错误,问题只出现在有两个参数的情况下。


1
重复的问题:在VBA中调用子程序……等。这个问题已经被多次回答过了。 - Jean-François Corbett
2个回答

58

当使用多个参数时,你可以写成:

 setInterest "myAccount", 3
或者
 Call setInterest("myAccount", 3)

在这两个例子中,你可以给参数命名:

setInterest account:="myAccount", dmonth:= 3

2

我添加了这个答案,来回答为什么你的语法只需要一个参数就可以工作?

Public Sub setInterest(account As String)
    '...somecode...
End Sub

setInterest ("myAccount")

注意:
当在()之间没有任何,时,VBA会认为它是一个公式并且只有一个参数。

当公式计算结果如下:

Dim str As String
str = ("TEST")
Debug.Print str

[Output:]
TEST

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接