Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass Array Into Subroutine for Excel VBA?

I’m trying to pass the results of an array into a subroutine. I have an array that picks up four different Buyer codes from a list. They’re labelled as BuyOne, BuyTwo, BuyThree, BuyFour. I’m trying to get the results into the next subroutine, but I’m getting a type mismatch at the subroutine call. Even when I fiddle with it I don’t know how to get the results into the subroutine. Can anyone tell me what i’m doing wrong?

Code below:

    lastRow = Range("O" & Rows.Count).End(xlUp).Row
    Set rBuyerList = Range("O1:O" & lastRow)
    arrBuyer = Array("BuyOne", "BuyTwo", "BuyThree", "BuyFour")
    
    For i = 0 To UBound(arrBuyer)
        With Application
            chkFind = .IfError(.Match(Range(arrBuyer(i)), Range("O1:O50"), 0), 0)
        End With
        
        If Range(arrBuyer(i)) = vbNullString Or chkFind = False Then
            MsgBox "Invalid Buyer Code.." & arrBuyer(i)
            Range(arrBuyer(i)).Select
        End If
    Next i

Call runFinished(sFrDt, sToDt, arrBuyer())

Sheets("Main Sheet").Select

MsgBox ("done...")


End Sub

Sub runFinished(sFrDt As String, sToDt As String, arrBuyer() As Variant)
Dim SQL As String

' add a new work sheet
ActiveWorkbook.Worksheets.Add

' dispay Criteria
Cells(1, 1) = "Run Date: " & Now()
Call MergeLeft("A1:B1")

Cells(2, 1) = "Criteria:"
Cells(2, 2) = "From " & Range("reqFrDT") & " -To- " & Range("reqToDt")


' SQL
SQL = "select a.StockCode [Finished Part], a.QtyToMake, FQOH,FQOO,/*FQIT,*/FQOA,  b.Component [Base Material], CQOH,CQOO,CQIT,CQOA " & _
"from ( " & _
"    SELECT StockCode, sum(QtyToMake) QtyToMake " & _
"    from [MrpSugJobMaster] " & _
"    WHERE 1 = 1 " & _
"    AND JobStartDate >= '" & sFrDt & "' " & _
"    AND JobStartDate <= '" & sToDt & "' " & _
"    AND JobClassification = 'OUTS' " & _
"    AND ReqPlnFlag <> 'I'  AND Source <> 'E' Group BY StockCode " & _
"    ) a " & _
"LEFT JOIN BomStructure b on a.StockCode = b.ParentPart " & _
"LEFT JOIN ( " & _
"            select StockCode, sum(QtyOnHand) FQOH, Sum(QtyAllocated) FQOO, Sum(QtyInTransit) FQIT, Sum(QtyOnOrder) FQOA " & _
"            from InvWarehouse " & _
"            where Warehouse in ('01','DS','RM') " & _
"            group by StockCode " & _
") c on a.StockCode = c.StockCode " & _
"LEFT JOIN ( " & _
"            select StockCode, sum(QtyOnHand) CQOH, Sum(QtyAllocated) CQOO, Sum(QtyInTransit) CQIT, Sum(QtyOnOrder) CQOA " & _
"            from InvWarehouse " & _
"            where Warehouse in ('01','DS','RM') " & _
"            group by StockCode " & _
") d on b.Component = d.StockCode "
SQL = SQL & _
"LEFT JOIN InvMaster e on a.StockCode = e.StockCode " & _
"WHERE 1 = 1 " & _
"and e.Buyer in  ('" & BuyOne & "','" & BuyTwo & "','" & BuyThree & "','" & BuyFour & "') " & _
"ORDER BY a.StockCode "

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you have this line in your code

arrBuyer = Array("BuyOne", "BuyTwo", "BuyThree", "BuyFour")

Proper call should be

Call runFinished(sFrDt, sToDt, arrBuyer)

And proper declaration of the function is

Sub runFinished(sFrDt As String, sToDt As String, arrBuyer As Variant) without ()
I’ll remove this answer in a few mins 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading