Hide Rows Based on Drop Down Selection

dgrimm

Board Regular
Joined
Sep 17, 2007
Messages
157
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

friel300

Board Regular
Joined
Jan 22, 2008
Messages
69
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
 

dgrimm

Board Regular
Joined
Sep 17, 2007
Messages
157
Thank you friel, I made the changes including selectionchange to change. Now it works great. So what dictates selectionchange vs change??
 

friel300

Board Regular
Joined
Jan 22, 2008
Messages
69
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,951
Messages
5,834,554
Members
430,295
Latest member
amdis

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
Top