+ Reply to Thread
Results 1 to 12 of 12

Hiding and Unhiding two sets of worksheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135

    Hiding and Unhiding two sets of worksheets

    My Workbook has 72 worksheets split into two. The first 36 include all the data, but the second 36 have one element removed.
    I am very grateful to RoyUK and others who have steered me towards some code that allows me to hide and unhide the first 36 sheets exactly as I want, but when I add the second 36 the code comes up with an error saying the Procedure is too large.
    I did want to split the code into two parts activated by two validated drop down menus, but this did not work either.

    If I have two drop down menus at D8 and G8 is it possible to have two separate codes as follows

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Count > 1 Or Target.Address <> "$D$8" Then Exit Sub
        Select Case Target.Value
            Case 1
                Sheets("1-Sheet1").Visible = xlSheetVisible
                Sheets("2-Sheet1").Visible = xlSheetHidden
                Sheets("2-Sheet2").Visible = xlSheetHidden
                Sheets("3-Sheet1").Visible = xlSheetHidden
                Sheets("3-Sheet2").Visible = xlSheetHidden
                Sheets("3-Sheet3").Visible = xlSheetHidden
    
            Case 2
                Sheets("1-Sheet1").Visible = xlSheetHidden
                Sheets("2-Sheet1").Visible = xlSheetVisible
                Sheets("2-Sheet2").Visible = xlSheetVisible
                Sheets("3-Sheet1").Visible = xlSheetHidden
                Sheets("3-Sheet2").Visible = xlSheetHidden
                Sheets("3-Sheet3").Visible = xlSheetHidden
    
            Case 3
                Sheets("1-Sheet1").Visible = xlSheetHidden
                Sheets("2-Sheet1").Visible = xlSheetHidden
                Sheets("2-Sheet2").Visible = xlSheetHidden
                Sheets("3-Sheet1").Visible = xlSheetVisible
                Sheets("3-Sheet2").Visible = xlSheetVisible
                Sheets("3-Sheet3").Visible = xlSheetVisible
    
            Case Else
        End Select
    End Sub
    The code is OK when I have 36 Worksheets and link this code to one drop down menu. When there are 72 Worksheets it fails. There must be a way to shorten the commands but
    Sheets(Array("2-Sheet1", "2-Sheet2")).Visible = True
    Does not work whereas
    Sheets(Array("2-Sheet1", "2-Sheet2")).Visible = False
    does!

    This question is linked to my previous post :-

    Can I attach a macro to a drop down list selection

    Regards

    Geoff
    Last edited by Glio; 10-14-2008 at 03:09 PM.
    Geoff Culbertson
    Petersfield, UK

  2. #2
    Forum Contributor
    Join Date
    06-14-2008
    Posts
    153
    Hi,

    If you can split your code in two separate macros, then you can try this:

    In the ListBox_Click event or ComboBox_Change event (depending on which one you are using as a dropdown), check for the Text property and run the relevant macro accordingly. Here is a sample code:
    Private Sub ListBox1_Click()
    If ListBox1.Text = "ABC" Then
      Application.Run ("macro1")
    Else
      Application.Run ("macro2")
    End If
    End Sub
    Regards.
    Welcome to: http://www.exceldigest.com/myblog/
    "Excel help for the rest of us"

  3. #3
    Registered User
    Join Date
    10-07-2008
    Location
    Wessex
    Posts
    55
    How about something like this

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Count > 1 Or Target.Address <> "$D$8" Then Exit Sub
        Select Case Target.Value
            Case 1
                SheetVisibility xlSheetVisible, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
    
            Case 2
            
                SheetVisibility xlSheetHidden, xlSheetVisible, xlSheetVisible, xlSheetHidden, xlSheetHidden, xlSheetHidden
    
            Case 3
                SheetVisibility xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetVisible, xlSheetVisible, xlSheetVisible
    
            Case Else
        End Select
    End Sub
    
    Private Sub SheetVisibility(sh1 As Long, sh2 As Long, _
                                sh3 As Long, sh4 As Long, _
                                sh5 As Long, sh6 As Long)
    
        Sheets("1-Sheet1").Visible = sh1
        Sheets("2-Sheet1").Visible = sh2
        Sheets("2-Sheet2").Visible = sh3
        Sheets("3-Sheet1").Visible = sh4
        Sheets("3-Sheet2").Visible = sh5
        Sheets("3-Sheet3").Visible = sh6
                                
    End Sub

  4. #4
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135
    Thanks very much for the responses from SE1429 and Martesoft. I was having trouble with a ComboBox so tried MarteSoft's idea and understanding it almost fully implemented it successfully, until I came across the inbuilt limit of 60 arguments for a procedure. Unfortunately I need 72.

    Microsoft Help tells me that I can do this with an array "You can also pass multiple arguments by placing them in an array"

    My working code is as follows with 36 Worksheets, but I need to add another set of 36 Worksheets in exactly the same format as the first 36.

    How can I implement an array to get round the 60 argument limit?

    Thanks again for all the ideas.


    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Count > 1 Or Target.Address <> "$F$8" Then Exit Sub
        
        Select Case Target.Value
            Case "1-Sheet1"
                SheetVisibility _
                xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
    
                
            Case "2-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
    
                
            Case "3-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
                
                
            Case "4-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
    
                
            Case "5-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
            
            
            
            Case "6-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
            
            Case "7-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden
            
            
            Case "8-Sheet"
                SheetVisibility _
                xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, xlSheetHidden, _
                xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible, xlSheetVisible
    
    Private Sub SheetVisibility(sh1 As Long, sh2 As Long, sh3 As Long, sh4 As Long, sh5 As Long, _
                                sh6 As Long, sh7 As Long, sh8 As Long, sh9 As Long, sh10 As Long, _
                                sh11 As Long, sh12 As Long, sh13 As Long, sh14 As Long, sh15 As Long, _
                                sh16 As Long, sh17 As Long, sh18 As Long, sh19 As Long, sh20 As Long, _
                                sh21 As Long, sh22 As Long, sh23 As Long, sh24 As Long, sh25 As Long, _
                                sh26 As Long, sh27 As Long, sh28 As Long, sh29 As Long, sh30 As Long, _
                                sh31 As Long, sh32 As Long, sh33 As Long, sh34 As Long, sh35 As Long, _
                                sh36 As Long)
    
        Sheets("1-Sheet1").Visible = sh1
        Sheets("2-Sheet1").Visible = sh2
        Sheets("2-Sheet2").Visible = sh3
        Sheets("3-Sheet1").Visible = sh4
        Sheets("3-Sheet2").Visible = sh5
        Sheets("3-Sheet3").Visible = sh6
        Sheets("4-Sheet1").Visible = sh7
        Sheets("4-Sheet2").Visible = sh8
        Sheets("4-Sheet3").Visible = sh9
        Sheets("4-Sheet4").Visible = sh10
        Sheets("5-Sheet1").Visible = sh11
        Sheets("5-Sheet2").Visible = sh12
        Sheets("5-Sheet3").Visible = sh13
        Sheets("5-Sheet4").Visible = sh14
        Sheets("5-Sheet5").Visible = sh15
        Sheets("6-Sheet1").Visible = sh16
        Sheets("6-Sheet2").Visible = sh17
        Sheets("6-Sheet3").Visible = sh18
        Sheets("6-Sheet4").Visible = sh19
        Sheets("6-Sheet5").Visible = sh20
        Sheets("6-Sheet6").Visible = sh21
        Sheets("7-Sheet1").Visible = sh22
        Sheets("7-Sheet2").Visible = sh23
        Sheets("7-Sheet3").Visible = sh24
        Sheets("7-Sheet4").Visible = sh25
        Sheets("7-Sheet5").Visible = sh26
        Sheets("7-Sheet6").Visible = sh27
        Sheets("7-Sheet7").Visible = sh28
        Sheets("8-Sheet1").Visible = sh29
        Sheets("8-Sheet2").Visible = sh30
        Sheets("8-Sheet3").Visible = sh31
        Sheets("8-Sheet4").Visible = sh32
        Sheets("8-Sheet5").Visible = sh33
        Sheets("8-Sheet6").Visible = sh34
        Sheets("8-Sheet7").Visible = sh35
        Sheets("8-Sheet8").Visible = sh36
    
    End Sub

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,468
    I would go with this approach.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Count > 1 Or Target.Address <> "$F$8" Then Exit Sub
        
        Select Case Target.Value
            Case "1-Sheet1"
                SheetVisibility 1
            Case "2-Sheet"
                SheetVisibility 2
            Case "3-Sheet"
                SheetVisibility 3
            Case "4-Sheet"
                SheetVisibility 4
            Case "5-Sheet"
                SheetVisibility 5
            Case "6-Sheet"
                SheetVisibility 6
            Case "7-Sheet"
                SheetVisibility 7
            Case "8-Sheet"
                SheetVisibility 8
            End Select
    
    End Sub
    
    Sub SheetVisibility(Item As Long)
    
        Dim lngIndex As Long
        Dim lngItem As Long
        
        For lngIndex = 1 To Item
            Sheets(Item & "-Sheet" & lngIndex).Visible = xlSheetVisible
        Next
        
        For lngItem = 1 To 8
            If lngItem = Item Then
                ' already done
            Else
                For lngIndex = 1 To lngItem
                    Sheets(lngItem & "-Sheet" & lngIndex).Visible = xlSheetHidden
                Next
            End If
        Next
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  6. #6
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135
    Andy,

    Thank you so much, that is so much simpler!!

    Your code tackles the first 36 of my worksheets, but the next 36 are Main1 to Main8, Main1 being 1 worksheet, Main2 being 2 worksheets etc.

    I would also like to have a Case showing all the Sheets, a Case showing all the Mains and a Case hiding all the worksheets.

    It just amazes me how many different ways of doing the same thing there are!

    Thanks again

+ 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. Unhiding and Hiding Worksheets based on a range of values
    By menor59 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2008, 12:33 AM
  2. Button For Hiding & Unhiding Rows
    By LAWDAWG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2008, 04:37 PM
  3. Hiding and unhiding rows - two scenarios
    By EdMac in forum Excel General
    Replies: 6
    Last Post: 02-08-2007, 02:16 PM
  4. Hiding and unhiding column automaticaly when a cell value changes
    By titovalle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2006, 09:08 PM
  5. Hiding or unhiding a picture
    By krabople in forum Excel General
    Replies: 3
    Last Post: 11-08-2006, 01:03 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