VBA: Argument not optional

28 浏览
0 Comments

VBA: Argument not optional

我第一次尝试使用VBA中的一些集合。我计划使用这个集合来打开多个报告并运行相同的一段代码,这就是为什么我想将它们放入一个集合中的原因。(如果有更好的方法,请告诉我。)

我的集合创建函数(用于返回集合):

Function CollectReports() As Collection
    Dim reports As New Collection
    reports.Add Item:="plant1", Key:="0"
    reports.Add Item:="plant2", Key:="1"
    reports.Add Item:="plant3", Key:="2"
    reports.Add Item:="plant4", Key:="3"
    TestCollection (reports)
End Function

我的集合测试子程序:

Sub TestCollection(reports As Collection)
    Dim x As Variant
    For Each x In reports
        MsgBox (x)
    Next
End Sub

我最初将子程序设为Sub TestCollection(ByRef reports),这是我在其他需要从另一个方法声明的方法中使用的方式。

我的问题是,当我尝试调试CollectReports()函数时,出现了一个Argument not optional错误。

如果你愿意的话,这是我计划使用这个集合的代码块 - 集合是这样做的最好方式吗?

Sub VlookupMGCCode(ByRef reports)
Dim lastrow As Integer
Dim wRange As Range
Dim blankRange As Range
Dim x As Variant
lastrow = Cells(Rows.count, "A").End(xlUp).Row
Set wRange = Range("$T$7:$T$" & lastrow) '用于vlookup的单列
CollectReports
For Each x in CollectReports '在这里肯定有一个错误
    Set blankRange = wRange.SpecialCells(xlCellTypeBlanks)
    blankRange.Formula = "=VLOOKUP(RC[-18],'[" & x & "]Sheet1'!C1:C31,31,FALSE)"
    With blankRange
        .FillDown
        .Copy
        .PasteSpecial Paste:=xlPasteValues, SkipBlanks:=False
    End With
Next
End Sub

我还没有尝试运行VlookupMGCCode()子程序,因为需要集合,所以我不知道可能会出现什么错误,但我相当有信心,我正在尝试使用CollectReports返回的集合的方式是错误的。

非常感谢你的帮助和时间!

0
0 Comments

VBA: Argument not optional的问题是一个常见的VBA错误,该错误通常出现在代码中的函数或子程序调用时。这个错误的原因是调用函数或子程序时没有提供必需的参数。

在上述给出的问题中,有两个问题导致了VBA: Argument not optional的错误:

1. 第一行代码:TestCollection (reports),这里的问题是在调用TestCollection函数时使用了括号。括号在VBA中表示函数的返回值,而不是参数。要解决这个问题,可以使用以下两种方法之一:

- Call TestCollection (reports)

- TestCollection reports

2. CollectReports函数缺少一个分配集合的代码行。在函数的最后需要添加以下代码行:

Set CollectReports = reports

以上就是VBA: Argument not optional错误出现的原因以及解决方法。这个错误通常是由于参数未被正确提供引起的。为了避免这个错误,我们需要确保在调用函数或子程序时提供所有必需的参数,并且不要在参数周围使用括号。

关于为什么某些变量可以直接赋值而另一些变量需要使用Set关键字,这是因为一个是简单值,而另一个是对对象的引用。简单值可以直接赋值,而对象引用需要使用Set关键字进行赋值。

希望以上解释对于理解VBA: Argument not optional错误以及如何避免这个错误有所帮助。如果您想了解更多关于VBA错误和解决方法的信息,可以参考上面提供的链接。

0
0 Comments

VBA: Argument not optional问题的出现原因是在调用子过程(TestCollection)时,使用了括号将参数(reports)括起来。这导致VBA解释器将参数视为可选参数,从而引发了“Argument not optional”的错误。

解决方法是在调用子过程(TestCollection)时去掉括号,即将TestCollection(reports)改为TestCollection reports。这样VBA解释器会将参数视为必需参数,就能成功调用子过程。

以下是修正后的代码示例:

Function CollectReports() As Collection
    Dim reports As New Collection
    reports.Add Item:="plant1", Key:="0"
    reports.Add Item:="plant2", Key:="1"
    reports.Add Item:="plant3", Key:="2"
    reports.Add Item:="plant4", Key:="3"
    TestCollection reports
End Function

请注意,如果TestCollection是一个函数而不是一个子过程,并且具有返回值,则需要使用tmp_var = TestCollection reports的形式来接收返回值。但根据评论中的说明,TestCollection是一个子过程,没有返回值,因此不需要进行赋值操作。

,为了避免VBA: Argument not optional错误,我们需要注意在调用子过程时不要使用括号将参数括起来。

0
0 Comments

(VBA: Argument not optional)这个问题的出现的原因是在使用CollectReports函数时没有传入必需的参数reports。解决方法是在调用CollectReports函数时传入reports参数。

在给出的代码示例中,CollectReports函数定义了一个参数reports,用于接收一个集合对象。在函数内部,通过调用reports对象的Add方法向集合中添加了四个元素。

在TestCollection子过程中,首先声明了一个名为reports的Collection对象,并使用CollectReports函数对其进行初始化。然后使用For Each循环遍历reports集合中的每个元素,并弹出一个消息框显示当前元素的值。

但是,在代码示例的后面部分,出现了一个错误的示例。在这个示例中,没有传入reports参数,并且直接使用了CollectReports函数和For Each循环。这样做是错误的,因为函数没有收到必需的参数。

要解决这个问题,需要在调用CollectReports函数时传入reports参数。修改后的代码如下:

CollectReports reports
For Each x In reports 'deffinately an error here

这样,CollectReports函数将会接收到reports参数,然后将元素添加到集合中。接下来,使用For Each循环遍历集合中的元素进行处理。

感谢提供答案!然而,在我的想法中,如果先运行函数,然后调用一个测试会更好。给出了一个更符合我的思路的解决方法,但还是谢谢 🙂 -- 这个回答适用于第一个帖子,而非编辑后的帖子。

0