如何根据另一个工作表上的单元格值隐藏/显示多个工作表中的行

时间:2019-03-26 15:44:35

标签: excel vba

我正在尝试提出一种更高效,更快速的运行方式来适应此公式,以便根据“输入”选项卡上D10的值在多个工作表中隐藏/显示相同的行,其中此宏是存储。

我已经尽力找到了所有可以找到的东西,但看起来我将不得不在第一个工作表的正下方列出与新工作表名称完全相同的行引用。

Private Sub Worksheet_Change(ByVal Target As Range)

Application.DisplayAlerts = False 'Prevents general pop-ups
Application.Calculation = xlManual 'Formulas are not calculated
Application.ScreenUpdating = False 'What the user see's on screen will not change

If Intersect(Target, Range("D10")) Is Nothing Then Exit Sub

'Hides all major city information so just the titles are shown.
If Target.Address = ("$D$10") And Target.Value = "" Then
Sheets("Weekly Report - New").Unprotect
    Sheets("Weekly Report - New").Rows("54:63").EntireRow.Hidden = True
    Sheets("Weekly Report - New").Rows("68:77").EntireRow.Hidden = True
    Sheets("Weekly Report - New").Rows("82:91").EntireRow.Hidden = True
    Sheets("Weekly Report - New").Rows("96:105").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("18:31").EntireRow.Hidden = False
            Sheets("Weekly Report - New").Rows("23:47").EntireRow.Hidden = True

'Unhides 'London' under the major cities section and keeps the rest hidden.
ElseIf Target.Address = ("$D$10") And Target = "UK" Then
Sheets("Weekly Report - New").Unprotect
    Sheets("Weekly Report - New").Rows("54").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("68").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("82").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("96").EntireRow.Hidden = False
            Sheets("Weekly Report - New").Rows("55:63").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("69:77").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("83:91").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("97:105").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("24:28").EntireRow.Hidden = False
                    Sheets("Weekly Report - New").Rows("18:23").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("29:47").EntireRow.Hidden = True

'Unhides 'French Riviera' & 'Paris' under the major cities section and keeps the rest hidden.
ElseIf Target.Address = ("$D$10") And Target = "France" Then
Sheets("Weekly Report - New").Unprotect
    Sheets("Weekly Report - New").Rows("55:56").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("69:70").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("83:84").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("97:98").EntireRow.Hidden = False
            Sheets("Weekly Report - New").Rows("54").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("68").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("82").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("96").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("57:63").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("71:77").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("85:91").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("99:105").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("30:34").EntireRow.Hidden = False
                    Sheets("Weekly Report - New").Rows("18:29").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("35:47").EntireRow.Hidden = True

'Unhides 'Barcelona' & 'Madrid' under the major cities section and keeps the rest hidden.
ElseIf Target.Address = ("$D$10") And Target = "Spain" Then
Sheets("Weekly Report - New").Unprotect
    Sheets("Weekly Report - New").Rows("57:58").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("71:72").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("85:86").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("99:100").EntireRow.Hidden = False
            Sheets("Weekly Report - New").Rows("54:56").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("59:63").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("68:70").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("73:77").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("82:84").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("87:91").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("96:98").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("101:105").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("36:40").EntireRow.Hidden = False
                    Sheets("Weekly Report - New").Rows("18:35").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("41:47").EntireRow.Hidden = True

'Unhides 'Florence', 'Maples', 'Milan', 'Rome' & 'Venice' under the major cities section and keeps the rest hidden.
ElseIf Target.Address = ("$D$10") And Target = "Italy" Then
Sheets("Weekly Report - New").Unprotect
    Sheets("Weekly Report - New").Rows("59:63").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("73:77").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("87:91").EntireRow.Hidden = False
    Sheets("Weekly Report - New").Rows("101:105").EntireRow.Hidden = False
            Sheets("Weekly Report - New").Rows("54:58").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("68:72").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("82:86").EntireRow.Hidden = True
            Sheets("Weekly Report - New").Rows("96:100").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("42:46").EntireRow.Hidden = False
                    Sheets("Weekly Report - New").Rows("18:41").EntireRow.Hidden = True
                    Sheets("Weekly Report - New").Rows("47").EntireRow.Hidden = True

End If

Sheets("Weekly Report - New").Rows("108:121").EntireRow.Hidden = True
Sheets("Weekly Report - New").Protect

Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub

对于“每周报告-新建”工作表,这很好(如果有点慢)。现在,我需要对“累积报告-新建”工作表执行相同的操作。

2 个答案:

答案 0 :(得分:1)

首先将true的隐藏和取消隐藏操作减少为false的隐藏和取消隐藏操作,以加快速度。例如

ws.Range("54:54,68:68,82:82,96:96,24:28").EntireRow.Hidden = False
ws.Range("55:63,69:77,83:91,97:105,18:23,29:47").EntireRow.Hidden = True

请注意,您必须在此处使用Range而不是Rows

并将代码放入过程中,以便可以将其重新用于任何工作表。请注意,您需要调整FranceSpainItaly

的代码
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.DisplayAlerts = False 'Prevents general pop-ups
    Application.Calculation = xlManual 'Formulas are not calculated
    Application.ScreenUpdating = False 'What the user see's on screen will not change

    If Intersect(Target, Me.Range("D10")) Is Nothing Then Exit Sub

    ' call the function
    HideAndUnhideRows ThisWorkbook.Worksheets("Weekly Report - New"), Me.Range("D10").Value
    HideAndUnhideRows ThisWorkbook.Worksheets("Cumulative Report - New"), Me.Range("D10").Value

    Application.DisplayAlerts = True
    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
End Sub

Private Sub HideAndUnhideRows(ws As Worksheet, Criteria As String)
    ws.Unprotect
    Select Case Criteria

    Case "" 'Hides all major city information so just the titles are shown.
        ws.Range("54:63,68:77,82:91,96:105,23:47").EntireRow.Hidden = True
        ws.Range("18:31").EntireRow.Hidden = False

    Case "UK" 'Unhides 'London' under the major cities section and keeps the rest hidden.
        ws.Range("54:54,68:68,82:82,96:96,24:28").EntireRow.Hidden = False
        ws.Range("55:63,69:77,83:91,97:105,18:23,29:47").EntireRow.Hidden = True

    Case "France" 'Unhides 'French Riviera' & 'Paris' under the major cities section and keeps the rest hidden.

    Case "Spain" 'Unhides 'Barcelona' & 'Madrid' under the major cities section and keeps the rest hidden.

    Case "Italy" 'Unhides 'Florence', 'Maples', 'Milan', 'Rome' & 'Venice' under the major cities section and keeps the rest hidden.

    End Select

    ws.Range("108:121").EntireRow.Hidden = True
    ws.Protect
End Sub

答案 1 :(得分:0)

由于要隐藏和取消隐藏很多行,所以我做了第一个示例,其余的可以用来做

Dim wb As Workbook, ws As Worksheet, SheetNames
Application.DisplayAlerts = False 'Prevents general pop-ups
Application.Calculation = xlManual 'Formulas are not calculated
Application.ScreenUpdating = False 'What the user see's on screen will not change

If Intersect(Target, Range("D10")) Is Nothing Then Exit Sub

Set wb = ThisWorkbook
SheetNames = Array("Weekly Report - New", "Cumulative Report - New")


StrValue = Target.Value
Select Case StrValue
    'Hides all major city information so just the titles are shown.
    Case vbNullString
        For i = 0 To 1
            Set ws = wb.Sheets(SheetNames(i))
            With ws
                .Unprotect
                Union(.Rows("54:63"), .Rows("68:77"), .Rows("82:91"), .Rows("96:105"), .Rows("23:47")). _
                    EntireRow.Hidden = True
                .Rows("18:31").EntireRow.Hidden = False
            End With
        Next i

    'All the other cases

End Select

我所做的每个工作表都有一个循环(假设两个工作表都具有相同的数据),并使用urdearboy关于联合的说法来提高可读性和更快的过程。

希望这会为您指明正确的方向。如果您需要其他帮助,请询问。