简体   繁体   中英

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. If the value is 0 all rows are to be hidden. If the value is 1 then rows 36 to 1000 are hidden. 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...

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...

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?

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. 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.

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. 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

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. You have to implement a logic using the Worksheet_Calculate event, as described at 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. 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. 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. Whether that value is produced by a formula or hard-coded value, the script will not care.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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