Hide Rows based on cell with drop down values

mboufleur

New Member
Joined
Feb 18, 2016
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I´m trying to activate a macro for my spreadsheet that automatically hides some rows based on a drop down list.

The drop down list has values such as "UHD-HDR" and "UHD-SDR", so I´m not sure they are causing the issue.

The code is the following, but nothing really happens

VBA Code:
Private Sub HideRowsBasedOnValues(ByVal Target As Range)

    Dim ws As Worksheet
    Dim cellValue As Range

    'Set reference to the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Change "Sheet1" to your sheet's name

    'Read the value from cell B22
    Set ws.cellValue = Range("B22")
    
    If Intersect(Target, cellValue) Is Nothing Then Exit Sub

    'Unhide all rows before applying hiding
    Rows("A29:A50").EntireRow.Hidden = False

    'Hide rows based on the value from cell B22
    Select Case ws.cellValue
            Case Is = ""
            Rows("A29:A50").EntireRow.Hidden = False
        Case Is = "UHD-HDR"
            Rows("A39:A50").EntireRow.Hidden = True
        Case Is = "UHD-SDR"
            Rows("A29:A38").EntireRow.Hidden = True
    End Select
End Sub

Could anyone help me understand why is this code not working? I´m using Excel 365 MSO Version 2307.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please try the following on a copy of your workbook. Right click the Sheet1 tab name, select View Code, and put the code in the window that appears on the right of the screen.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B22"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Rows("29:50").Hidden = False
        Select Case Target.Value
            Case Is = "UHD-HDR"
                Rows("39:50").Hidden = True
            Case Is = "UHD-SDR"
                Rows("29:38").Hidden = True
        End Select
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,569
Members
453,054
Latest member
arz007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top