【发布时间】: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