简体   繁体   English

VBA 根据更改的单元格值隐藏和取消隐藏行

[英]VBA Hide and Unhide Rows based on Changing Cell Values

I am trying to hide/unhide rows in excel based on a specific cell value.我正在尝试根据特定的单元格值隐藏/取消隐藏 excel 中的行。 If the value is 0 all rows are to be hidden.如果值为 0,则所有行都将被隐藏。 If the value is 1 then rows 36 to 1000 are hidden.如果值为 1,则隐藏第 36 到 1000 行。 If the value is 2 then rows 72 to 1000 are hidden, if it is 3 then 108 to 100 are hidden, etc until all cells can be unhidden...如果值为 2,则隐藏 72 到 1000 行,如果值为 3,则隐藏 108 到 100,依此类推,直到可以取消隐藏所有单元格...

Here is what I have so far... It works for hiding/unhiding, but if I change the number from 0 to 1 and then from 1 to 2 it does not update sometimes...这是我到目前为止所拥有的......它适用于隐藏/取消隐藏,但如果我将数字从0更改为1,然后从1更改为2,它有时不会更新......

Private Sub Worksheet_Change(ByVal Target As Range)
     If Target.Address = ("$E$3") And Target.Value = 0 Then
         Sheets("Abutments").Rows("5:1000").EntireRow.Hidden = True
     ElseIf Target.Address = ("$E$3") And Target.Value = 1 Then
        Sheets("Abutments").Rows("36:1000").EntireRow.Hidden = True
     ElseIf Target.Address = ("$E$3") And Target.Value = 2 Then
         Sheets("Abutments").Rows("72:1000").EntireRow.Hidden = True
     Else
         Sheets("Abutments").Rows("5:1000").EntireRow.Hidden = False
     End If
End Sub

BONUS is there a way for the vba code to reference the changing cell if the cell contained a formula?如果单元格包含公式,则 VBA 代码是否可以引用更改的单元格?

Your code is missing the part where rows are set back to visible that where hidden previously.您的代码缺少行被设置回可见的部分,而之前隐藏的部分是可见的。 So for example if you enter 1, rows 5...1000 are hidden.例如,如果您输入 1,则第 5...1000 行将被隐藏。 Now if you change the value to 2, you're hiding rows 36...1000 (but they are already hidden), but you don't display the rows 5..35.现在,如果您将值更改为 2,您将隐藏第 36...1000 行(但它们已被隐藏),但不会显示第 5..35 行。

The following code calculates the first row to be hidden.以下代码计算要隐藏的第一行。 All rows above that are displayed, all rows starting from this row until row 1000 are hidden.显示上面的所有行,从该行开始到第 1000 行的所有行都被隐藏。 If the math doesn't match exactly your needs, it will be easy to change to adapt it.如果数学与您的需求不完全匹配,则可以轻松更改以适应它。

Note that I used the Val function to prevent run time errors if the user enters something that is non-numerical请注意,如果用户输入非数字内容,我使用Val函数来防止运行时错误

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$E$3" Then Exit Sub

    Dim startrow As Long
    If Val(Target.Value) <= 0 Then
        startrow = 5
    Else
        startrow = Val(Target.Value) * 32
    End If

    With Sheets("Abutments")
        If startrow > 5 Then
            .Rows("5:" & startrow - 1).Hidden = False
        End If
        If startrow <= 1000 Then
            .Rows(startrow & ":1000").Hidden = True
        End If

    End With
End Sub

Update I don't know if I understand your bonus question correctly.更新我不知道我是否正确理解您的奖金问题。 If cell E3 contains a formula, the Change event will not be triggered when it's value changes.如果单元格E3包含公式,则其值更改时不会触发Change事件。 You have to implement a logic using the Worksheet_Calculate event, as described at https://stackoverflow.com/a/11409569/7599798您必须使用Worksheet_Calculate事件实现逻辑,如https://stackoverflow.com/a/11409569/7599798 所述

You almost had it!你几乎拥有它! Your logic was just slightly off:你的逻辑有点不对劲:

As a true believer in the KISS principle, just make a small tweak to the order of your statements.作为 KISS 原则的忠实信徒,只需稍微调整一下您的陈述顺序即可。 No need to over complicate this...没必要把这复杂化……

Private Sub Worksheet_Change(ByVal Target As Range)
 Sheets("Abutments").Rows("5:1000").EntireRow.Hidden = False ' Move this to the top
 If Target.Address = ("$E$3") And Target.Value = 0 Then
     Sheets("Abutments").Rows("5:1000").EntireRow.Hidden = True
 ElseIf Target.Address = ("$E$3") And Target.Value = 1 Then
    Sheets("Abutments").Rows("36:1000").EntireRow.Hidden = True
 ElseIf Target.Address = ("$E$3") And Target.Value = 2 Then
     Sheets("Abutments").Rows("72:1000").EntireRow.Hidden = True
 End If
End Sub

Just move your final ELSE statement condition to the beginning of your function.只需将最后的 ELSE 语句条件移动到函数的开头即可。 This will un-hide everything at the start, and then hide the rows based off of your selection.这将在开始时取消隐藏所有内容,然后根据您的选择隐藏行。 This will force your script to reevaluate the condition to hide rows every time, instead of having to meet a condition to un-hide the rows (which is why your original script was only working sometimes).这将迫使您的脚本每次都重新评估隐藏行的条件,而不必满足取消隐藏行的条件(这就是为什么您的原始脚本有时只工作的原因)。

EDIT:编辑:

Your bonus question is already solved with this script.你的奖金问题已经用这个脚本解决了。 As long as the value of the cell ( E3 in this case) contains a numeric value, it will hide the rows.只要单元格的值(在本例中为E3 )包含一个数值,它就会隐藏行。 Whether that value is produced by a formula or hard-coded value, the script will not care.无论该值是由公式生成还是由硬编码值生成,脚本都不会关心。

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

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