简体   繁体   English

根据值隐藏和取消隐藏行

[英]Hide and unhide rows based on a value

I am trying to create a survey in Excel and want to hide and unhide rows based on their answers.我正在尝试在 Excel 中创建一个调查,并希望根据他们的答案隐藏和取消隐藏行。 For example, if D3 = "no" hide rows D4:D10, and I want to repeat this multiple times throughout, but the number of rows to hide changes.例如,如果 D3 = "no" 隐藏行 D4:D10,并且我想在整个过程中多次重复此操作,但要隐藏的行数会发生变化。 So if D3 = "yes" leave unhidden.所以如果 D3 = "yes" 不隐藏。 Then move to answer D5, if D5 = "no" hide rows D6:D7.然后移动到回答 D5,如果 D5 =“否”隐藏行 D6:D7。 And this continues on and on throughout.这一直持续下去。

Using the worksheet_change() event.使用worksheet_change()事件。 In your VBE double click the worksheet where this change (this cell) will happen.在您的 VBE 中,双击将发生此更改(此单元格)的工作表。 Then enter:然后输入:

Private Sub Worksheet_Change(ByVal Target As Range)

    'Detect if the worksheet change was on cell D3
    If Not (Intersect(Target, Range("D3")) Is Nothing) Then
        'Hide rows if the value of D3 is "No"
        Range("D4:D10").EntireRow.Hidden = (Range("D3").Value = "No")
    End If
End Sub

Every time a change happens on this worksheet this subroutine will fire off, test if the change occured in cell "D2" then toggle the row's hidden property based on the value.每次在此工作表上发生更改时,都会触发此子例程,测试更改是否发生在单元格“D2”中,然后根据值切换行的隐藏属性。

The Hidden property of the EntireRow range object takes a True or False value, so we are able to just set it equal to the result of the conditional statement (Range("D3").value = "No") which will return a True or False greatly simplifying the amount of code you need to write. EntireRow范围对象的Hidden属性采用TrueFalse值,因此我们可以将其设置为等于条件语句(Range("D3").value = "No")的结果,这将返回TrueFalse大大简化了您需要编写的代码量。

You'll just need to add more If Not (Intersect(Target, Range("whatever")) Is Nothing) Then lines to test for your other cells like D5 and hide the appropriate rows base on whatever value you are testing for in that cell.您只需要添加更多If Not (Intersect(Target, Range("whatever")) Is Nothing) Then行来测试您的其他单元格(如D5 )并根据您正在测试的任何值隐藏相应的行细胞。

If that gets to be too much code (testing all those intersects) you can just test the intersects once like:如果代码太多(测试所有这些相交),您可以像这样测试一次相交:

If Not (Intersect(Target, Union(Range("D3"), Range("D5"), Range("D8"))) Is Nothing) Then
    'And then in here your individual lines that toggle the hidden property:
    Range("D4:D10").EntireRow.Hidden = (Range("D3").Value = "No")

    Range("D6:D7").EntireRow.Hidden = (Range("D5").Value = "No")

    ...
End If

Lastly, because this beast will fire any time any change is made on this worksheet, you may want to shut off event firing while the subroutine is running.最后,因为这个野兽会在任何时候在此工作表上进行任何更改时触发,您可能希望在子例程运行时关闭事件触发。 So turn enableEvents off at the top of the subroutine and turn it back on at the end:因此,在子例程的顶部关闭enableEvents ,并在末尾将其重新打开:

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Not (Intersect(Target, Union(Range("D3"), Range("D5"), Range("D8"))) Is Nothing) Then            
        Range("D4:D10").EntireRow.Hidden = (Range("D3").Value = "No")    
        Range("D6:D7").EntireRow.Hidden = (Range("D5").Value = "No")
        Range("D9:D12").EntireRow.Hidden = (Range("D8").Value = "No")    
    End If

    Application.EnableEvents = True

End Sub

This will prevent this same subroutine getting called by itself while it's busy changing the sheet causing an infinite loop and locking up excel (which sucks if you didn't save the workbook before firing it off).这将防止同一子例程在忙于更改工作表时被自己调用,从而导致无限循环并锁定 excel(如果您在启动工作簿之前没有保存工作簿,这很糟糕)。

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

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