Hide Rows Based on Drop Down Selection

dgrimm

Board Regular
Joined
Sep 17, 2007
Messages
159
Hey Folks,

I have a selection change problem. I am trying to hide rows based on the selection from a drop down (named QtrSel sitting in cell "C2"). I have visited several sites and looked at several questions here to see if I can figure out what is going on but to no avail. The code follows:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Address = "C2" Then
    
        Select Case Target
        
            Case "Q1"
                Rows("6:18").Hidden = False
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = True
                
            Case "Q2"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = False
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = True
                
            Case "Q3"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = False
                Rows("46:58").Hidden = True
                
            Case "Q4"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = False
                
        End Select
        
    End If

End Sub

Any ideas

Thank you

Dave
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Are you refering to the fact it dosent run when you select C2?
If so the address VBA sees for C2 is $C$2 so your if statement is always false.
If you add .address(False,False) instead of just address this will fix your problem.

also i would have thought it would be better to use a Worksheet Change Event instead of a Selection Change this way it will run when you change cell C2

but thats just my opinion :p

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address[B](False, False)[/B] = "C2" Then
    
        Select Case Target
        
            Case "Q1"
                Rows("6:18").Hidden = False
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = True
                
            Case "Q2"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = False
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = True
                
            Case "Q3"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = False
                Rows("46:58").Hidden = True
                
            Case "Q4"
                Rows("6:18").Hidden = True
                Rows("19:31").Hidden = True
                Rows("32:45").Hidden = True
                Rows("46:58").Hidden = False
                
        End Select
        
    End If
End Sub

Hope this helps
 
Upvote 0
Thank you friel, I made the changes including selectionchange to change. Now it works great. So what dictates selectionchange vs change??
 
Upvote 0
Sorry for the late reply pal.

Selection Change Event triggers when the user (or macro) selects a different cell.
Change Events Triggers when you actualy change content of a cell.

Your exmple will run the code every time you click into Cell C2, but that means when you change it you will select another cell to accept the changes then have to go back to C2 to run your code again.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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