I'm trying to mess around with some collections in VBA for the first time. I'm planning on using this collection to open multiple reports and run the same bit of code, which is why I wanted to get them into a collection. (If there's a better way to do that, please let me know.)
My collection making Function (to return the collection?):
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
My collection testing Sub:
Sub TestCollection(reports As Collection)
Dim x As Variant
For Each x In reports
MsgBox (x)
Next
End Sub
I originally had the sub as Sub TestCollection(ByRef reports)
which is what I've been using for other methods which require a Dim from another method.
My issue is that I get an Argument not optional
error when I attempt to debug my CollectReports()
function
If you're feeling really generous, here's the code block I'm planning on using this collection for - is a collection the best way to do this?
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
I haven't tried running the VlookupMGCCode()
Sub yet as the collection is needed, so I don't know what errors might come up, but I'm pretty confident that the way I'm trying to use the collection which CollectReports
returns is wrong.
Thank you very much for your help and time!