在宏中传递来自vlookup的MULTIPLE值

时间:2014-12-01 06:25:18

标签: vb.net vba excel-vba excel-2010 excel

我在Excel中创建了一个名为' DisplayColumnsRows'指的是= Sheet1!$ C $ 6:$ E $ 36

Column C = list of worksheet names (e.g Control)
Column D = list of columns or range to display (e.g A,B,C:H)
Column E = list of rows or range to display (e.g 1,2,5:10)

我创建了一个宏来查看名称中的每一行' DisplayColumnsRows'如果列C显示工作表名称(不是空白),我希望它隐藏该工作表中的所有列和行,然后取消隐藏/显示列D中的特定列和列E中的行。

到目前为止,我已经创建了以下代码,但我仍然坚持如何将值传递给ws.Range以显示列和行。

Sub Run_Me_To_Fix_Columns()
   Dim ws As Worksheet

'List the names of the worksheets to exclude from Sub resizingColumns

    Const excludeSheets As Variant = "Control/DIVA_Report/Asset_Details"

'Perform the following for each worksheet not excluded
    For Each ws In ActiveWorkbook.Worksheets

        If IsError(Application.Match(ws.Name, Split(excludeSheets, "/"), 0)) Then
        Call displayColumnRow(ws)
        End If

    Next
End Sub


Sub displayColumnRow(ws As Worksheet)

    Dim DisplayColumns       As Variant
    Dim DisplayRows          As Variant
    Dim myrange

    Set myrange = Worksheets("Control").Range("range_hideColumnRow")

    'Hide All Columns and Rows
    ws.Columns.EntireColumn.Hidden = True
    ws.Columns.EntireRow.Hidden = True

    'Lookup Worksheet name and identify columns & rows to display
    DisplayColumns = Application.VLookup(ws.Name, myrange, 2, False)
    DisplayRows = Application.VLookup(ws.Name, myrange, 3, False)

    'Display Columns and Rows (Unhide)
    If Not IsError(DisplayColumns) Then
    ws.Range("DisplayColumns").EntireColumn.Hidden = False
    'MsgBox DisplayColumns
    End If

    If Not IsError(DisplayRows) Then
    ws.Range("DisplayRows").EntireRow.Hidden = False
    'MsgBox DisplayRows
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

您只需要更改输入要显示的行和列的方式 - 始终输入列或行作为范围,即使它是单个列。例如,代替A,B,C:H执行此操作:A:A,B:B,C:H