Search code examples
excelvbavariablesworksheet

How to Pass a Worksheet variable to another sub


I have a main sub (Sub Macro5) that's calling a private sub (NamedRanges). (only part of the code added below)

I've declared and set the workbook and worksheet in the main sub as wb and wSh, since the names will constantly change I use them as variables.

I'm having a problem in using these variables in the private sub that's getting called. The error comes up in the private sub at With myWorksheet.Cells error:"Object variable or with variable not set" I presume the problem comes in at setting the worksheet name?

'identify worksheet containing cell range
    Set myWorksheet = wSh

It's the first time I'm trying to use variables in different subs. I've looked online but not sure how to solve this issue. Below is the private sub and part of the main sub code.

Private Sub NamedRanges(wb As Workbook, wSh As Worksheet)

'declare object variable to hold reference to worksheet containing cell range
    Dim myWorksheet As Worksheet

    'declare variables to hold row and column numbers that define named cell range (dynamic)
    Dim myFirstRow As Long
    Dim myLastRow As Long

    'declare object variable to hold reference to cell range
    Dim myNamedRangeDynamicVendor As Range
    Dim myNamedRangeDynamicVendorCode As Range

    'declare variable to hold defined name
    Dim myRangeNameVendor As String
    Dim myRangeNameVendorCode As String

    'identify worksheet containing cell range
    Set myWorksheet = wSh

    'identify first row of cell range
    myFirstRow = 2

    'specify defined name
    myRangeNameVendor = "namedRangeDynamicVendor"
    myRangeNameVendorCode = "namedRangeDynamicVendorCode"

    'Vendor Name range
    With myWorksheet.Cells

        'find last row of source data cell range
        myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

        'specify cell range
        Set myNamedRangeDynamicVendor = .Range(.Cells(myFirstRow, "A:A"), .Cells(myLastRow, "A:A"))

    End With
End Sub




Sub Macro5

Dim wb As Workbook
Dim ws As Worksheet
Dim path As String
Dim MainWB As Workbook
Dim MasterFile As String
Dim MasterFileF As String

Set ws = Application.ActiveSheet
Set MainWB = Application.ActiveWorkbook

Application.ScreenUpdating = False

'Get folder path
path = GetFolder()

'Get visible worksheet on Master data File
MasterFile = Dir(path & "\*Master data*.xls*")
MasterFileF = path & "\" & MasterFile

Set wb = Workbooks.Open(MasterFileF)

'Count visible worksheets
Dim i As Integer
Dim wSh As Worksheet

i = 0

For Each ws In wb.Worksheets
    If ws.Visible = True Then
        i = i + 1
    End If
Next ws

'if more then 1 sheet visible then prompt to choose one
If i > 1 Then
    MsgBox "More than one worksheet visible, please edit 'Master data' File to have only the 1 worksheet visible that it needs to use, and rerun macro"
    Exit Sub
Else
'If only 1 sheet visible use sheet name
    Set wSh = ws
End If

'Set Vendor Name and Code Range names
Call NamedRanges(wb, wSh)

I've included a lot of code for the main sub to show how I get my wb and wSh variables if it helps.


Solution

  • As per your code you seem to want wSh set to the first and only visible sheet in wb

    hence change the last part of Macro5 to:

    For Each ws In wb.Worksheets
        If ws.Visible = True Then
            Set wSh = ws
            i = i + 1
        End If
    Next ws
    
    'if more then 1 sheet visible then prompt to choose one
    If i > 1 Then
        MsgBox "More than one worksheet visible, please edit 'Master data' File to have only the 1 worksheet visible that it needs to use, and rerun macro"
        Exit Sub
    End If
    
    'Set Vendor Name and Code Range names
    Call NamedRanges(wb, wSh)