Macro for hiding rows and columns based on selection in drop down list

hallhill

New Member
Joined
Feb 2, 2018
Messages
4
Hi,
I need help with the macro vba coding....

My tab in the workbook is titled "Worksheet"
In cell A9, I have data validation set up where the user can select from a drop down list that has 4 values (Box, Bag, Ramp, Drive, null value)
If the user selects Box or the value is null in cell $A$9, I don't want the macro to do anything.
If the user selects Bag, Ramp, or Drive from the drop down list in cell $A$9 I want prevent them from making changes or selections in cells C12-C20 and E33-E83. I would even be happy if there was a reminder comment or something that appeared if they selected Bag, Ramp, or Drive, that said "do not make a selection in cells C12-C20 or E33-E88 based on your selection in cell A9.
NOTE: C12-C20 and E33-E83 also have data validation set up where the user can only select from a drop down list.

I considered having a macro that would hide rows 12-20 and hide column E if the user selected Bag, Ramp, or Drive from cell $A$9. But I am not sure how to write the VBA code for that.

Anyone able to assist? Thanks so much....M
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make a selection in A9. This will hide the rows and column E. You will have to decide how and when you would want the rows and column visible again.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A9")) Is Nothing Then Exit Sub
    Select Case Target.Value
        Case "Bag", "Ramp", "Drive'"
            ActiveSheet.Rows("12:20").Hidden = True
            Columns("E").EntireColumn.Hidden = True
            MsgBox "You cannot make a selection in cells C12-C20 or E33-E88 based on your selection in cell A9."
    End Select
End Sub
 
Upvote 0
Hi, I am getting a debug error when it gets to the Row that I have in bold, underline below. The Run-time error '1004': Unable to set the Hidden property of the Range class


Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A9")) Is Nothing Then Exit Sub
Select Case Target.Value
Case "Bag", "Ramp", "Drive'"
ActiveSheet.Rows("12:20").Hidden = True
Columns("E").EntireColumn.Hidden = True
MsgBox "You cannot make a selection in cells C12-C20 or E33-E88 based on your selection in cell A9."
End Select
End Sub
 
Upvote 0
I think I figured out why it was failing....I have all the cells in the worksheet locked (except for those fields that I want the user to make changes to) and it is password protected. When I removed the password and tested it the macro worked.

I have to protect the cells I don't want the user to make changes to...if I change the setting in the protection to allow format changes to rows and columns it appears to work fine.

Would it be possible to add to the coding the ability to unhide rows 12-20 and column E if the user changes their selection in A9 to 'box" or if they leave that cell blank?
 
Upvote 0
Try this macro. It hides/unhides the rows and column.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A9")) Is Nothing Then Exit Sub
    Select Case Target.Value
        Case "Bag", "Ramp", "Drive'"
            ActiveSheet.Rows("12:20").Hidden = True
            Columns("E").EntireColumn.Hidden = True
            MsgBox "You cannot make a selection in cells C12-C20 or E33-E88 based on your selection in cell A9."
        Case "Box", ""
            ActiveSheet.Rows("12:20").Hidden = False
            Columns("E").EntireColumn.Hidden = False
    End Select
End Sub
If you want to leave the rows and columns to be always visible, we could instead lock/unlock the fields that the user changes. If this is better for you, let me know and I'll modify the macro.
 
Last edited:
Upvote 0
Hi,
I love the suggestion of just lock/unlock the fields that the user can change based on their selection in cell A9.

One note, there are other fields in the worksheet that should be left unlocked or locked regardless of the selection in A9....but the two areas that are not required and should be left blank if the user selects Bag, Ramp or Drive includes cells B12-20 and E34-E83.

Do you mind sending me that macro code while I try the last one you sent?
 
Upvote 0
Try this macro. Make sure that cell A9 and ranges B12-20 and E34-E83 are unlocked. Change "mypassword" in the code (2 occurrences) to suit your needs.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A9")) Is Nothing Then Exit Sub
    ActiveSheet.Unprotect Password:="mypassword"
    Select Case Target.Value
        Case "Bag", "Ramp", "Drive'"
            Range("C12:C20,E33:E83").Locked = True
            MsgBox "You cannot make a selection in cells C12-C20 or E33-E88 based on your selection in cell A9."
        Case "Box", ""
            Range("C12:C20,E33:E83").Locked = False
    End Select
    ActiveSheet.Protect Password:="mypassword"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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