VBA Code to hide/unhide rows when a something is selected from drop down list.

Essencex

New Member
Joined
Aug 7, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi I cannot figure this out for the life of me!
I need a code that will unhide rows 8-47 when 'Yes - Answer questions below' is selected in drop down list of cell E7.

The same goes for the following on the same sheet:
I need a code that will unhide rows 57-91 when 'Yes - Answer questions below' is selected in drop down list of cell E56.
*I guess I can just copy and paste the code for above and change it slightly for the second request?

hopefully someone can help :-D
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
@Essencex Welcome
Try like this Change Event code. Paste into the sheet's code pane.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Target = "Yes - Answer questions below" Then Exit Sub
    Select Case Target.Address
    Case "$E$7"
        Range("E8:E47").Rows.EntireRow.Hidden = False
        
    Case "$E$56"
        Range("E57:E91").Rows.EntireRow.Hidden = False
    Case Else
    End Select
    
End Sub
HTH
 
Upvote 0
Hi Tony, this absolutely worked thank you!! Ive tested it and near perfect...how do I, if the word 'Yes - Answer questions below' is de-selected from list how do i get it back to 're-hiding' the rows?
 
Upvote 0
Hi, Try like..

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim HideRows As Boolean
    If Target.Count > 1 Then Exit Sub
    HideRows = Not Target = "Yes - Answer questions below"
    Select Case Target.Address
    Case "$E$7"
        Range("E8:E47").Rows.EntireRow.Hidden = HideRows
        
    Case "$E$56"
        Range("E57:E91").Rows.EntireRow.Hidden = HideRows
    Case Else
    End Select
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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