Home » excel » excel – Passing worksheet as argument to a sub routine

excel – Passing worksheet as argument to a sub routine

Posted by: admin April 23, 2020 Leave a comment

Questions:

I know there are many questions out there on this topic yet I am still struggling with a subscript out of range? Yes the workbook I am importing does have a sheet entitled LSL Recon, I verified. I have debugged and substituted LSL Recon with Import Sheets(1) (as in Sheet1) and then the procedure does continue a bit further but does not import anything into the array.

Option Explicit

Public FILENAME, c_DATE, cNAME As String
'Public ws As Worksheet

Sub main()
    Application.DisplayAlerts = True
    Application.ScreenUpdating = False

    CLEAR
    Import Sheets("LSL Recon")
    Display_Import
End Sub()

Sub Import(ws As Worksheet)  
    Workbooks.Open FILENAME
    Set TempBook = ActiveWorkbook
    ws.Activate

    cNAME = "Entity"
    cA = Sheets(1).Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cNAME = "Sector"
    cB = Sheets(1).Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cNAME = "Date"
    cC = Sheets(1).Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cNAME = "Client"
    cD = Sheets(1).Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    ...
End Sub()

Helpful Questions:
VBA: Calling a sub on another worksheet with multiple arguments
pass sheet to a function (excel vba)
Passing a Worksheet to a subroutine

How to&Answers:

Get rid of your public scope variables, declare ALL variables, and pass arguments as needed:

Option Explicit

Sub main()

    Dim FILENAME$
    Dim c_DATE$
    Dim cNAME$
    Dim wsName$
    wsName = "LSL Recon"
    Application.DisplayAlerts = True
    Application.ScreenUpdating = False

    CLEAR
    Import (wsName)
    Display_Import
End Sub

Sub Import(wsName$)  
    Dim wb as Workbook
    Dim cNames, itm, found
    ' Use an array of items to search for
    cNames = Split("Entity,Sector,Date,Client",",")

    Set wb = Workbooks.Open(FILENAME)

    Set ws = wb.Sheets(wsName)

    For Each itm in cNames
        found = ws.Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
        Debug.Print cName " found in column: " & found
    Next

End Sub

If you need to return the results of the .Find to the main procedure, then change this to a Function, and return a collection object, and call it like so:

Set foundItems = Import(wsName)
Dim itm
For each itm in foundItems
    Debug.Print itm
Next

Then the function:

Function Import(wsName$)  
    Dim wb as Workbook
    Dim ret as New Collection
    Dim cNames, itm, found
    ' Use an array of items to search for
    cNames = Split("Entity,Sector,Date,Client",",")

    Set wb = Workbooks.Open(FILENAME)

    Set ws = wb.Sheets(wsName)

    For Each itm in cNames
        ret.Add ws.Rows.Find(What:=UCase(cNAME), LookAt:=xlWhole, SearchDirection:=xlNext).Column
        Debug.Print cName " found in column: " & ret(ret.Count)
    Next
    'return the collection to the calling procedure
    Set Import = ret
End Function