简体   繁体   中英

Unhide rows based on a numeric value in a cell

I am trying to create Worksheet_Change() event to unhide rows based on the numeric value of one cell.

I have a table B13:B513 . I want to unhide a number of rows equal to the value in C7 . For example, if C7 = 10, then B13:B22 will unhide and the rest will still be hidden.

I have seen several ways of doing something similar but using "case" for each option. In my case I have 500 options. I am sure there is a more efficient way of doing this.

I am using Excel 2010.

Thanks in advance!

Daniel

The code below on Worksheet_Change() event, will run only if the user changes the value in Cell " C7 ", you can modify it easily in the code (it is commeneted at which line).

The code wil unhide the number of rows specified starting from row 13 (according to Range B13:B513 )

Private Sub Worksheet_Change(ByVal Target As Range)

Dim WatchRange                  As Range
Dim IntersectRange              As Range
Dim Numof_UnhideRows            As Long
Dim UnhideRowStart              As Long


' if you want to unhide the rows in Range(B13:B513) only when someone canges the vlues in C7
Set WatchRange = Range("C7")

' starting unhiding number of rows starting for row 13
UnhideRowStart = 13

Set IntersectRange = Intersect(Target, WatchRange)

If Not IntersectRange Is Nothing Then
    If IsNumeric(Target.Value) Then
        Numof_UnhideRows = Target.Value
        Rows(UnhideRowStart & ":" & UnhideRowStart + Numof_UnhideRows - 1).EntireRow.Hidden = False

        MsgBox "Unhide a total of " & Numof_UnhideRows & " rows"
    Else
        MsgBox "Cell C7 doesn't contain a Numeric Value", vbCritical
    End If
Else
   'Do Nothing Spectacular

End If

End Sub

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