简体   繁体   English

如何根据多个单元格值自动隐藏和取消隐藏excel中的行

[英]How to automatically hide and unhide rows in excel based on multiple cell value

enter image description here I'm a newbie to excel macro.在此处输入图像描述我是 excel 宏的新手。 I'm trying to create a macro to automatically hide and unhide rows in excel based on multiple cell value.我正在尝试创建一个宏来根据多个单元格值自动隐藏和取消隐藏 excel 中的行。 I have created two drop down cells with value using data validation and based on the drop down I'm trying to hide and unhide.我使用数据验证创建了两个具有值的下拉单元格,并基于下拉列表我试图隐藏和取消隐藏。 If the first drop down is "Class I" and second drop down value is Ä only row from 8 to 20 should be visible and row from 21 to end of the rows to be hidden and similarly when the first drop down is "Class I" and second drop down value is "B" row from 8 to end of rows should be hidden except for rows from 23 to 31. If require I can provide the excel file it contains dummy data如果第一个下拉列表是“I 类”而第二个下拉值是 Ä 则只有从 8 到 20 的行应该是可见的,并且从 21 到要隐藏的行末尾的行应该是可见的,同样当第一个下拉列表是“I 类”时第二个下拉值是“B”行,从 8 到行尾应该隐藏,除了从 23 到 31 的行。如果需要,我可以提供包含虚拟数据的 excel 文件

Put this code in the worksheet's private code sheet accessed though right-click worksheet name tab, View Code.将此代码放在通过右键单击工作表名称选项卡查看代码访问的工作表的私有代码表中。 Modify the row ranges to suit.修改行范围以适应。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("D3, D4")) Is Nothing Then
        On Error GoTo meh
        Application.ScreenUpdating = False
        Range("8:" & Me.UsedRange.Rows.Count).EntireRow.Hidden = True
        Select Case Range("D3").Value2
            Case "Class I"
                Select Case Range("D4").Value2
                    Case ChrW(196)  'Ä
                        Intersect(Range("8:20"), Me.UsedRange).EntireRow.Hidden = False
                    Case "B"
                        Intersect(Range("21:33"), Me.UsedRange).EntireRow.Hidden = False
                    Case Else
                        'do nothing
                End Select
            Case "Class II"
                'copy above for Class I here
                'modify rows to show/hide
            Case Else
                'do nothing
        End Select
    End If

meh:
    Application.ScreenUpdating = True

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM