+ Reply to Thread
Results 1 to 8 of 8

Hide/Unhide Worksheets based on drop down

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2015
    Location
    Houghton-Le-Spring, England
    MS-Off Ver
    2010
    Posts
    16

    Hide/Unhide Worksheets based on drop down

    Hi,

    I'm new to the forum and also fairly new to VBA.

    I would appreciate any help with this issue. I have several Sheets within a workbook named - Header, Select Test Type, Test 1 Details, Test 1, Test 2 Details, Test 2, etc..... up to test 10. I want the header sheet and select test type to always remain visible and the rest to be hidden, then on the select test type sheet I have a data validation drop down box with all the test names. I would like to be able to select the test from this to then make the corresponding sheets visible.

    eg. I select test 1 then Test 1 details sheet and test 1 sheet appear.

    Can anyone help with this please

    I've attached a sample of the workbook

    much appreciated

    If it matters I'm using office 2016
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Hide/Unhide Worksheets based on drop down

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    ' goes in the "Select Test Type" worksheet class module
    ' right click on the sheet tab and post the code
    
    If Intersect(Target, Range("B3")) Is Nothing Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    
    Sheets(Array(Target.Value, Target.Value & " details")).Visible = False
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-06-2015
    Location
    Houghton-Le-Spring, England
    MS-Off Ver
    2010
    Posts
    16

    Re: Hide/Unhide Worksheets based on drop down

    Hi TMS,

    Thanks for the extremely quick response

    I've copied over the code but when I select the corresponding test number from the dropdown box it hides the sheets, but I require the test number selected to be the only visible ones.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Hide/Unhide Worksheets based on drop down

    Hi Kerplunk1039,

    Welcome to the forum!!

    Assign this macro to the Go button on the Select Test Type tab of your workbook:

    Option Explicit
    Sub Macro1()
    
        Dim wsMySheet As Worksheet
        Dim strMySelection As String
        
        Application.ScreenUpdating = False
        
        strMySelection = ActiveSheet.Range("B3")
        
        For Each wsMySheet In ThisWorkbook.Sheets
            If wsMySheet.Name = "Header" Or wsMySheet.Name = "Select Test Type" Then
                wsMySheet.Visible = xlSheetVisible
            ElseIf InStr(wsMySheet.Name, strMySelection) > 0 Then
                wsMySheet.Visible = xlSheetVisible
            Else
                wsMySheet.Visible = xlSheetHidden
            End If
        Next wsMySheet
        
        Application.ScreenUpdating = True
    
    End Sub
    Note you will then have to save the workbook as an Excel Macro-Enabled Workbook (*.xlsm).

    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Registered User
    Join Date
    11-06-2015
    Location
    Houghton-Le-Spring, England
    MS-Off Ver
    2010
    Posts
    16

    Re: Hide/Unhide Worksheets based on drop down

    Hi Robert,

    Thank you very much, that is working great,

    Just to expand on this is it possible to put something in there that when the file is opened only the header sheet and select test type sheet are visible.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Hide/Unhide Worksheets based on drop down

    Hi Kerplunk1039,

    Just to expand on this is it possible to put something in there that when the file is opened only the header sheet and select test type sheet are visible.
    You can use this (almost identical) code from my first post:

    Option Explicit
    Private Sub Workbook_Open()
    
        Dim wsMySheet As Worksheet
        
        Application.ScreenUpdating = False
        
        strMySelection = ActiveSheet.Range("B3")
        
        For Each wsMySheet In ThisWorkbook.Sheets
            If wsMySheet.Name = "Header" Or wsMySheet.Name = "Select Test Type" Then
                wsMySheet.Visible = xlSheetVisible
            Else
                wsMySheet.Visible = xlSheetHidden
            End If
        Next wsMySheet
        
        Sheets("Select Test Type").Select 'Default tab upon opening. Change to suit if necessary.
        
        Application.ScreenUpdating = True
    
    End Sub
    Just note it goes in the ThisWorkbook module of your workbook not a blank module like my above code.

    Regards,

    Robert

  7. #7
    Registered User
    Join Date
    11-06-2015
    Location
    Houghton-Le-Spring, England
    MS-Off Ver
    2010
    Posts
    16
    This is extremely helpful and works great.
    Thank you so much.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Hide/Unhide Worksheets based on drop down

    Oops, sorry, misread and went for a quick solution

    Looks like you have an answer now though.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA macro to hide/unhide particular worksheets on selection from drop-down menu
    By lealea1982 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2014, 09:51 AM
  2. Hide/Unhide Rows Based On Drop Down Selection
    By QABrian in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-14-2014, 11:53 PM
  3. Hide/Unhide cells based on drop-down list
    By Savvy25 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2014, 02:36 AM
  4. Hide-Unhide rows on multiple worksheets based on value of a drop down list
    By clo2peter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-10-2014, 08:32 AM
  5. [SOLVED] Hide/Unhide columns in multiple worksheets based on item chosen in a drop down list.
    By Gattaca2014 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-27-2014, 01:31 PM
  6. [SOLVED] How to hide & unhide rows based on selection from drop down box?
    By jgomez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2013, 08:30 PM
  7. Replies: 1
    Last Post: 02-01-2013, 03:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1