简体   繁体   English

如果范围包含某些值,则隐藏特定行

[英]Hide specific rows if range contains certain values

I would like to have some help with automatizing my data sheet. 我希望在自动执行数据表方面有所帮助。

In the range of E7:V7 (hereinafter, r) I have same drop down lists, each of them has four different values ("-"; "open"; "close"; "both"). 在E7:V7(以下为r)的范围内,我具有相同的下拉列表,每个下拉列表均具有四个不同的值(“-”;“打开”;“关闭”;“两者”)。

When r contains only "-" , I would like to have rows 21:50 hidden. r仅包含"-" ,我想隐藏21:50行。

  • "open" shows rows 21:30 "open"显示第21:30
  • "close" shows rows 31:50 "close"显示第31:50
  • "both" shows rows 21:50 "both"显示第21:50

For example: 例如:

  • if E7= "-", F7="open", then rows 21:30 are shown and 31:50 hidden. 如果E7 =“-”,F7 =“ open”,则显示21:30行,而31:50隐藏。
  • if E7="-", F7="both", then all rows are shown. 如果E7 =“-”,F7 =“ both”,则显示所有行。

I hope it was clear enough. 我希望它足够清楚。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("E7").Value = "-" Then
        Rows("21:50").EntireRow.Hidden = True
    ElseIf Range("E7").Value = "open" Then
        Rows("21:30").EntireRow.Hidden = False
        Rows("31:50").EntireRow.Hidden = True
    ElseIf Range("E7").Value = "close" Then
        Rows("31:50").EntireRow.Hidden = False
        Rows("21:30").EntireRow.Hidden = True
    ElseIf Range("E7").Value = "both" Then
        Rows("21:50").EntireRow.Hidden = False
    End If
End Sub

This code works only for one criteria, but I hope it helps to clarify the situation. 该代码仅适用于一个条件,但我希望它有助于弄清情况。

As suggested in my comment: Use the Application.Intersect method , as well as the Select Case statement . 如我的注释中所建议:使用Application.Intersect方法以及Select Case语句

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim AffectedCells As Range
    Set AffectedCells = Intersect(Target, Target.Parent.Range("E7:V7"))

    If Not AffectedCells Is Nothing Then
        Dim Cell As Range
        For Each Cell In AffectedCells
            Select Case Cell.Value
                Case "-"
                    Target.Parent.Rows("21:50").Hidden = True
                Case "open"
                    Target.Parent.Rows("21:30").Hidden = False
                    Target.Parent.Rows("31:50").Hidden = True
                Case "close"
                    Target.Parent.Rows("31:50").Hidden = False
                    Target.Parent.Rows("21:30").Hidden = True
                Case "both"
                    Target.Parent.Rows("21:50").Hidden = False
            End Select
        Next Cell
    End If
End Sub

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

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