+ Reply to Thread
Results 1 to 22 of 22

hide/show multiple sheets by macro ?

  1. #1
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82

    hide/show multiple sheets by macro ?

    Hi folks,

    I got a quite huge excel file with multiple sheets. For convenience sake I want to group and hide all the sheets not necessary for the viewer.
    I would like to do it like this:
    Please Login or Register  to view this content.
    ' deselect a range or a few defined sheets (don't know if you can define a 'range' with sheets ?)
    Please Login or Register  to view this content.
    Any suggestions ? The minimum I'd like to work is not having to 'manually' let the macro select all the sheets they have to hide, as they are too many.
    Cheers,

    Simon
    Last edited by Simon-ch; 10-30-2008 at 09:38 AM.

  2. #2
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    accidental reply.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You can't hide all the sheets in a workbook.

    Your second example will work, provided not all the sheets are selected. You can do

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    it was supposed to be 1 example :-) Just didn't format it properly:
    1. Select all sheets
    2. Deselect the ones I want to show
    3. Hide all selected sheets

    Hope it's clear now... selecting all the sheets by name is just too much work, plus there might be additions.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Here's a userform to list all sheets & hide the ones that are selected
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Another version.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  7. #7
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Thanks for the reply.. but I can't quite figure out how to adapt this user form to my purposes, and it seems to hide the sheets with data instead of the ones that are empty.
    (Sheet8,5,2 are shown, the rest hidden)
    Is there no easier way to do this ? Maybe another approach:
    I would like them to click a button and it just hides all the sheets except the ones that start with the same letters:
    [KGM] <-- if you click this userform it shows all sheets with KGM_texttest*


    edit: @ Andy:

    Thanks, this seems to be a solution, but the code has a problem at 'sht.Select blnReplace' it wants me to debug it.
    Last edited by Simon-ch; 10-29-2008 at 11:20 AM.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Thanks, but it always returns with a request for debugging because of the same line in the code:
    sht.Select blnReplace
    I don't really know what it does, so cannot debug it really

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    What do you mean no data in them, completely empty?

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    The code simply selects a sheet. The blnReplace variable tells it whether to replace currently selected sheets with latest, in effect ungrouping, or not.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Well I just opened the "Sheetstohide" workbook, clicked on print and it would hide the ones that aren't empty and show the empty ones.
    But it isn't really relevant anyway, because it's not the function I am looking for, so sorry for the distraction.
    I can't quite seem to get the above code to work, it always stops at
    "sht.Select blnReplace" no idea what's wrong with it.
    Thanks to you 2 for the help !

    Simon

  13. #13
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Your sheet works just the way I'd like it to work, how can I now hide the selected sheets ?

    When I cope the same code into my workbook it still has the same debugging problem, but what it apparently does is select the first 15 sheets or so, the rest not.
    Last edited by Simon-ch; 10-29-2008 at 11:58 AM.

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    At the end of the routine you would use the code you first posted.

    Please Login or Register  to view this content.

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The userform only lists sheets with data in, then you select from it to hide.

    This example lists all the sheets
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Ok, works perfectly in the test sheet but still refuses to work with my own workbook :-(
    Could the naming of my sheets be a problem ? Got quite long sheet names with spaces..
    It still points to "sht.Select blnReplace"
    when I return to the workbook it has selected the first 14 sheets.
    Last edited by Simon-ch; 10-29-2008 at 12:19 PM.

  17. #17
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do you have a sheet with KMG in it's name?

  18. #18
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Try stepping through the code line by line, use F8 in vbe.

    When it errors use this in the immediate window.

    Please Login or Register  to view this content.
    This will tell you the sheet name that fails. Is it a worksheet/chartsheet?

    Failing that can you post the offending workbook? You can strip all the data off the sheets if needs be.

  19. #19
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    If Roy's suggestion is true then this will handle that case.

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Ok, I think I figured out why it turned up with an error. I have 2 sheets hidden in the middle and in the end of the workbook. I kind of need the one in the end hidden because I want to make a sum through all the sheets, even if new ones are added, so I named the last sheet "End" with =sum(start:end!g59)
    so I don't need to adjust the sum when new sheets are added.
    So the code works perfectly without hidden sheets, thanks for your patience, do you have any idea about my new (altough minor) problem ?

    Simon
    Last edited by Simon-ch; 10-30-2008 at 04:42 AM.

  21. #21
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Did the code actually raise an error? If so details of that error would have helped isolate this problem.

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Yes, sorry, it all works perfectly without hidden sheets really. But I would like to always keep the "End"-sheet hidden, I could just put it in every macro.
    But this is the code I have now, works well

    Sub KGM()
    Dim WS As Worksheet
    For Each WS In Worksheets
    WS.Visible = True
    Next
    ' I have implemented this because if I used the macro before it would have hidden sheets of course, so unhide all before any macro can be executed.
    Dim sht As Object
    Dim blnReplace As Boolean
    Dim blnAllSheets As Boolean

    blnReplace = True
    blnAllSheets = True
    For Each sht In ActiveWorkbook.Sheets
    If UCase(Left(sht.Name, 3)) = "KGM" Then
    ' leave alone
    blnAllSheets = False
    Else
    sht.Select blnReplace
    blnReplace = False
    End If
    Next

    If blnAllSheets Then
    MsgBox "Can not hide all sheets", vbExclamation
    Else
    ActiveWindow.SelectedSheets.Visible = xlSheetHidden
    Sheets("Gesamtkosten").Visible = xlSheetVisible
    Sheets("Gesamtkosten").Select
    End If
    'This is because I always want to have "Gesamtkosten" shown, not very elegant again, but easy code for slow learners ;-)
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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