如何将工作表变量传递给另一个子

How to Pass a Worksheet variable to another sub

我有一个主子 (Sub Macro5) 正在调用一个私有子 (NamedRanges)。 (下面只添加了部分代码)

我已在主子中声明并设置工作簿和工作表为 wbwSh,因为名称会不断变化我将它们用作变量。

我在被调用的私有 sub 中使用这些变量时遇到问题。错误出现在 With myWorksheet.Cells 的私有子目录中 错误:"Object variable or with variable not set" 我认为问题出在设置工作表名称时?

'identify worksheet containing cell range
    Set myWorksheet = wSh

这是我第一次尝试在不同的潜艇中使用变量。我在网上看过,但不确定如何解决这个问题。下面是私有子代码和部分主子代码。

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)

我已经包含了很多主子代码来展示我如何获得我的 wbwSh 变量(如果有帮助的话) .

我认为你的 Sub NamedRanges 你已经声明了 wSh As Worksheet 并且你不需要再次声明它 'identify worksheet containing cell range Set myWorksheet = wSh

你最好直接用 'Vendor Name range With wSh.Cells

抱歉,由于我的特权,我还不能发表评论:)

根据您的代码,您似乎希望 wSh 设置为 wb

中第一个且唯一可见的 sheet

因此将 Macro5 的最后部分更改为:

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)