Show/Hide Several Sheets based on Data Validation

SwampManJim

New Member
Joined
Aug 11, 2017
Messages
3
Greetings, new user who is not a VBA pro and would appreciate help.

I'm also working on a navigate sheets ribbon, which I can post/reciprocate when I finish.

I'm working on a workbook, where the user will select from a list (datavalidation) with a range name "AssetType", and based on their choice sheets will unhide.

I've got maybe 10 items in the datavalidation and have 10 columns with the sheet names I want per choice. I can name these ranges.

I need help after the "then", to loop through my asset types.

For "Vehicle" show the sheets named in range "VehicleSheets"
For "Hand Tool" show the sheets named in Range "HandTools"
etc....

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("AssetType")) Is Nothing Then
If Range("AssetType").Value = "Vehicle" And Not IsEmpty(Range("AssetType")) Then


Me.Visible = xlSheetHidden
Else
Me.Visible = xlSheetVisible


End If


End If
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I dont know where this code is running from, however I recommend that you run this code in the data validation drop box macro. This way you dont have to loop through each option, instead you can create case logic for each value selected.

For showing and hiding the sheet:

Code:
[COLOR=#303336][FONT=inherit]ActiveWorkbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Sheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Name"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Visible [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] xlSheetVeryHidden or xlSheerHidden or xlSheetVisible[/FONT][/COLOR]

Note: "xlSheetVeryHidden" will make the sheet hidden and only visible again through vba code.

Cheers,

AKR22
 
Upvote 0
I dont know where this code is running from, however I recommend that you run this code in the data validation drop box macro. This way you dont have to loop through each option, instead you can create case logic for each value selected.

For showing and hiding the sheet:

Code:
[COLOR=#303336][FONT=inherit]ActiveWorkbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Sheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Name"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]).[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Visible [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] xlSheetVeryHidden or xlSheerHidden or xlSheetVisible[/FONT][/COLOR]

Note: "xlSheetVeryHidden" will make the sheet hidden and only visible again through vba code.

Cheers,

AKR22

So Is "Name" where I put my range name (MachinerySheets which refers to a list of sheet names in B2:B10)?

Sorry for the newb questions, I'm trying to pick this up as fast as possible
 
Upvote 0
So this is what I have so far:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AssetType")) Is Nothing Then
        Select Case Range("AssetType")
            Case "Vehicle": Macro1
            Case "HandTools": Macro2
            Case "Machinery": Macro3
            Case "LeasedEquipment": Macro4
                  End Select
    End If
    End Sub

Now I need the code for each "Macro1,2,3,4" to unhide a list of sheets that are in a named range
 
Upvote 0
No, your sheet name goes there. No range needed either, you are making a change to the sheet. If you're working in the same worksheet you can just say:
Code:
[COLOR=#303336][I]Active[/I][/COLOR][COLOR=#303336][I]Sheet[/I][/COLOR][COLOR=#303336][I].[/I][/COLOR][COLOR=#303336][I]Visible [/I][/COLOR][COLOR=#303336][I]=[/I][/COLOR][COLOR=#303336][I] xlSheetVeryHidden or xlSheetHidden or xlSheetVisible[/I][/COLOR]

Otherwise:

Code:
[COLOR=#303336][I]ActiveWorkbook.Work[/I][/COLOR][COLOR=#303336][I]Sheets("sheetnamehere")[/I][/COLOR][COLOR=#303336][I].[/I][/COLOR][COLOR=#303336][I]Visible [/I][/COLOR][COLOR=#303336][I]=[/I][/COLOR][COLOR=#303336][I] xlSheetVeryHidden or xlSheetHidden or xlSheetVisible[/I][/COLOR]

AKR22
 
Upvote 0
All you need now is to put this into each case:


Code:
[FONT=Verdana]For Each cell In Range(nameofrangehere)

[/FONT][FONT=Verdana]     Worksheets(cell).Visible = xlSheetHidden

[/FONT]
[FONT=Verdana]Next cell[/FONT]

Be sure to write the declare the named range at the beginning of the sub.

AKR22
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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