Forum Discussion

Knimmo's avatar
Knimmo
Copper Contributor
Oct 17, 2023
Solved

Hiding Rows with VBA based on a dropdown List

Hi,   I am looking for a VBA code to hide rows that are greater than a value in a dropdown list. For example, in the below image, since the drop down shows 3, i would like the rows for 4-10 to be h...
  • HansVogelaar's avatar
    Oct 17, 2023

    Knimmo 


    Right-click the sheet tab.
    Select 'View Code' from the context menu.
    Copy the code listed below into the code window.
    Close the Visual Basic Editor.
    Save the workbook as a macro-enabled workbook (*.xlsm).
    Make sure that you allow macros when you open it.
    Code:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim n As Long
        Dim m As Long
        If Not Intersect(Range("C4"), Target) Is Nothing Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            m = Range("B:B").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            n = Val(Range("C4").Value)
            Range("A7:A" & m).EntireRow.Hidden = False
            If n >= 0 And n < m - 7 Then
                Range("A" & n + 7 & ":A" & m).EntireRow.Hidden = True
            End If
            Application.EnableEvents = True
            Application.ScreenUpdating = True
        End If
    End Sub

Resources