简体   繁体   English

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

[英]How to hide / show rows across multiple worksheets based on a cell value on another worksheet

I am trying to come up with a more efficient, faster running way to adapt this formula so that it hides / shows the same rows across multiple worksheets based on the value of D10 on the 'Input' tab, where this macro is stored.我正在尝试提出一种更有效、更快的运行方式来调整此公式,以便它根据存储此宏的“输入”选项卡上 D10 的值在多个工作表中隐藏/显示相同的行。

I have tried everything I can find but it is looking like I will have to list out the exact same row references with the new worksheets name directly below the 1st worksheet.我已经尝试了我能找到的所有方法,但看起来我必须在第一个工作表正下方列出具有新工作表名称的完全相同的行引用。

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

This works fine (if a bit slow) for the "Weekly Report - New" worksheet.这适用于“每周报告 - 新”工作表(如果有点慢)。 I now need it to do the same for the "Cumulative Report - New" worksheet.我现在需要它为“累积报告 - 新”工作表做同样的事情。

First of all reduce your hide and unhide actions to one for true and one for false to speed it up.首先,将隐藏和取消隐藏操作减少为truefalse一个以加快速度。 For Example例如

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

Note you must use Range instead of Rows here.请注意,您必须在此处使用Range而不是Rows

And put your code into a procedure so you can re-use it for any sheet.并将您的代码放入一个过程中,以便您可以在任何工作表中重复使用它。 Note that you need to adjust the code for France , Spain and Italy注意需要调整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

Since there are a ton of different rows to hide and unhide I did the first example which you can use to do the rest:由于有大量不同的行要隐藏和取消隐藏,我做了第一个示例,您可以用它来完成其余的工作:

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

What I did there was a loop for each worksheet you need to work in (assuming both have the same data) and using what urdearboy said about the union for more readability and faster process.我所做的是为您需要处理的每个工作表创建一个循环(假设两者都具有相同的数据),并使用 urdearboy 所说的关于联合的内容来提高可读性和更快的处理速度。

Hope this points you in the right direction.希望这为您指明了正确的方向。 IF you need anymore help just ask.如果您需要更多帮助,请直接询问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个工作表中的单元格值隐藏一个工作表中的行 - Hide rows in one worksheet based on a cell value in another worksheet 根据另一个工作表的单元格输出隐藏/取消隐藏另一个工作表中的多行的 VBA 代码 - VBA code to hide/unhide multiple rows in another worksheet based on cell output from another worksheet 使用VBA根据不同工作表中的单元格值隐藏工作表中的行 - Hide rows in a worksheet based on a cell value in different worksheet using VBA VBA:CountIfS函数,用于计算多个工作表中单元格值为Yes的次数IF另一个单元格值 - VBA: CountIfS function to count how many times a cell value is Yes across multiple worksheets IF another cell value Excel VBA 根据表中的单元格值显示/隐藏工作表行 - Excel VBA Show/Hide worksheet rows based on cell values in table 使用 VBA,基于单个单元格隐藏多行,该单元格为空但具有指向另一个工作表代码的链接 - Using VBA, hide multiple rows based on a single cell that is empty but has a link to another worksheet code 如何基于多个行的单元格值合并跨单元格 - How to Merge Cells Across Based on Cell's Value for Multiple Rows 如何通过excel中的工作表索引号对多个工作表中的相同单元格求和? - How can you sum the same cell across multiple worksheets by worksheet index number in excel? 基于另一个单元格值隐藏/取消隐藏行 - Hide/Unhide rows based on another cell value 如何根据多个单元格值自动隐藏和取消隐藏excel中的行 - How to automatically hide and unhide rows in excel based on multiple cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM