【问题标题】:VBA: Argument not optionalVBA:参数不是可选的
【发布时间】:2015-09-28 02:01:09
【问题描述】:

我第一次尝试在 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 设置为 Sub TestCollection(ByRef reports),这是我一直用于其他需要 Dim 来自其他方法的方法的方法。


我的问题是当我尝试调试 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) 'a single column to vlookup

CollectReports

For Each x in CollectReports 'deffinately an error here
    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() Sub,因为需要集合,所以我不知道可能会出现什么错误,但我对我尝试使用集合的方式很有信心CollectReports 返回的是错误的。


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

【问题讨论】:

    标签: vba excel collections


    【解决方案1】:

    请参阅this answer,了解何时使用方括号。

    你有几个问题:

    1. 这行需要改一下:

      TestCollection (reports)

      对任何一个

      Call TestCollection (reports)

      TestCollection reports

    2. 您的CollectReports 函数缺少分配集合的代码行。您需要在函数结束之前添加这一行:

      Set CollectReports = reports

    【讨论】:

    • 嘿,谢谢你的回答。在Set x = value的思路上,你能解释一下为什么Dim x As Integer之类的东西可以用x=1设置,而Dim w As Range之类的范围需要Set w as Range吗?
    • 因为一个是简单的值,另一个是对对象的引用。见this answerthis one
    【解决方案2】:

    我认为错误报告具有误导性, 您实际上在 TestCollection(Reports) 处收到错误,您在调用 sub 时不需要括号。如果可行,请尝试删除并反馈

    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
    

    【讨论】:

    • 是的,去掉括号。 Excel 如何处理括号内的参数真的很奇怪。对象需要在括号之外传递,否则 excel 会以不同的方式对待它们。这真的很奇怪。正如另一条评论中所说,使用 Call 关键字也会改变它们的处理方式。
    • TestCollection (reports) 期望将其设置为一个值。像 tmp_var = TestCollection (reports) 如果我没记错的话
    • 但 TestCollection 是子非函数 - 没有返回值。为什么要分配?
    【解决方案3】:

    做事有点不对劲

    Function CollectReports(reports As Collection) As 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"
    
    End Function
    
    Sub TestCollection()
        Dim reports As New Collection
    
        CollectReports reports
    
        Dim x As Variant
    
        For Each x In reports
            MsgBox (x)
        Next
    
    End Sub
    

    至于你的其他代码,我还没有全部测试过

    CollectReports
    
    For Each x in CollectReports 'deffinately an error here
    

    改成

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

    【讨论】:

    • 感谢您的回答!但是,如果我运行函数然后调用测试而不是相反,它在我的脑海中会更好。 @Krishna 给出了一个更符合我思路的解决方案,但仍然,谢谢:) - 这适用于第一篇文章,而不是编辑:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多