+ Reply to Thread
Results 1 to 9 of 9

VBA to hide/ unhide worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-11-2015
    Location
    melbourne
    MS-Off Ver
    2010
    Posts
    38

    VBA to hide/ unhide worksheets

    I have some code that hides and unhides a worksheet in excel. I have attached my spreadsheet for reference test.xlsm.

    So far if I click yes for 'Investigations" the worksheet 'site details' appears and if I click no it disappears.

    My problem is that if I click no for another category e.g. 'dispersive soils' then I don't want the 'site details' sheet to disappear as it is ticked as a yes in another category.

    Is there code that can help me?

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to hide/ unhide worksheets

    i suggest firstly naming your option buttons to something you can work easily with
    naming the first one yes and the other ones 1-5 really don't make sense

    what you need is a code to check if any of the "yes" options are true

    Sub CheckOption()
        
        Dim ctl As Object
        Dim Hide As Boolean 'check to hide site details
        
        Hide = True 'set to hide to true by default
        Application.ScreenUpdating = False 'stop flickering screen
        
        For Each ctl In Worksheets("Job Components").OLEObjects
            If TypeOf ctl.Object Is MSForms.OptionButton Then
                If Application.WorksheetFunction.IsEven(Right(ctl.Name, 1)) Then 'only check the even option buttons
                    
                    If ctl.Object.Value = True Then 'if any of option "yes" is true then hide to false
                        Hide = False
                    End If
                
                End If
            End If
        Next ctl
        
        If Hide = True Then
            Worksheets("Site Details").Visible = False
        Else
            Worksheets("Site Details").Visible = True
        End If
        
        Application.ScreenUpdating = True 'turn screenupdating back on
        
    End Sub
    then make all the optionbuttons call this sub
    Attached Files Attached Files
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    05-11-2015
    Location
    melbourne
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA to hide/ unhide worksheets

    Thanks for the quick reply I will give it ago and rename my option buttons also.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to hide/ unhide worksheets

    if you have more than 9 buttons
    i suggest you name 1-9 with a leading zero
    ie01,02 and updating the IF to read

    If Application.WorksheetFunction.IsEven(Right(ctl.Name, 2))
    also you dont have to use even or odd just something readily easy to split
    like YesOpt1, NoOpt1,YesOpt2 ...etc then split check left(ctl.name,3) = yes
    Last edited by humdingaling; 10-13-2015 at 09:58 PM.

  5. #5
    Registered User
    Join Date
    05-11-2015
    Location
    melbourne
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA to hide/ unhide worksheets

    I tried to put the code to use best I could and being fairly new to VBA I am not sure what it all means. I have attached the updated version of the worksheet and it is working much better. Some of the worksheets aren't closing properly when no is clicked and wonder if I have used the code correctly.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to hide/ unhide worksheets

    oh wow you have been busy

    you didnt take my advice from Post #4
    you now have more than 9 option buttons
    running this line of code
    If Application.WorksheetFunction.IsEven(Right(ctl.Name, 1))
    it will only take the single digit at the end
    this would be fine but ...for 10,20,30 these would become zero

    this aside i think you have misunderstood how the code works

    this for loop
        For Each ctl In Worksheets("Job Components").OLEObjects
            If TypeOf ctl.Object Is MSForms.OptionButton Then
                If Application.WorksheetFunction.IsEven(Right(ctl.Name, 1)) Then 'only check the even option buttons
                    
                    If ctl.Object.Value = True Then 'if any of option "yes" is true then hide to false
                        Hide = False
                    End If
                
                End If
            End If
        Next ctl

    goes thru every single oleobject (commandbutton,option button, etc) on the spreadsheet
    then checks to see if that object is an option button
    then checks the last character of the name of the option button to see if it is even
    then checks to see if it is true...
    if it passes all these then it applys the "hide" (Boolean which a variable you can set to true or false) to true

  7. #7
    Registered User
    Join Date
    05-11-2015
    Location
    melbourne
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA to hide/ unhide worksheets

    Ok thanks for the help I will have a play with it all and change the numbering. Although I might have taken on a task that beyond my excel skills.

  8. #8
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to hide/ unhide worksheets

    sorry i may have confused you

    the method i am showing you creates a code which every single button can use instead of having code for every single button individually
    the reason for this is if you have to change something you only need to change one set of codes instead of micro change every individual button

    you need to map out exactly what set of option buttons does what (ie which button/group of buttons hides which sheet)
    then just plot outcomes against each one
    i can help you with the syntax but not the outcomes

  9. #9
    Registered User
    Join Date
    05-11-2015
    Location
    melbourne
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA to hide/ unhide worksheets

    Thanks I will keep trying.

+ 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. Hide and Unhide Worksheets
    By ExcelNel101 in forum Excel General
    Replies: 5
    Last Post: 03-06-2015, 06:15 AM
  2. Hide Unhide Worksheets VBA
    By Playemgraeme in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-30-2014, 04:31 PM
  3. [SOLVED] PW Protected Macros Needed to 1) Hide Certain Worksheets & 2) Unhide Worksheets & Columns
    By Aimee S. in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2014, 10:49 AM
  4. [SOLVED] Can hide/unhide or add worksheets
    By govcam in forum Excel General
    Replies: 5
    Last Post: 04-08-2014, 08:20 PM
  5. Help with VBA Code (Hide/Unhide Worksheets)
    By maryren in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2014, 12:43 PM
  6. hide and unhide worksheets
    By pankaj2145 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-27-2009, 07:26 PM
  7. [SOLVED] Hide and/or Unhide Worksheets
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2006, 04:35 PM

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