+ Reply to Thread
Results 1 to 5 of 5

Hide/unhide sheets as per list in named range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Hide/unhide sheets as per list in named range

    Hi
    I wonder can help me. I have the following sheets within my workbook:

    Interface
    properties M60
    properties M61
    properties M62
    properties M64
    properties general

    On sheet "properties general" in cells G1 to G7 I have the following values:

    G1 ---- Sheets (properties)
    G2 ---- All sheets

    G3 ---- properties M60
    G4 ---- properties M61
    G5 ---- properties M62
    G6 ---- properties M64
    G7 ---- properties general


    Now I have created a dynamic damed range (Sheet_count_DYNAMIC_Properties). This named range essentially covers G3 to G7 (highlighted in green).

    My question is, how to code in VBA to hide all sheets declared in named range Sheet_count_DYNAMIC_Properties???. I have attached example xls.
    Any help is much appreciated.
    Attached Files Attached Files
    Last edited by rain4u; 02-18-2012 at 01:18 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Hide/unhide sheets as per list in named range

    Hi rain4u, try the following two procedures:
    Sub hide_properties()
    
        Dim Ws As Worksheet, Sht As Worksheet
        Dim dRng As Range, Rng As Range
        
        Set Ws = Worksheets("properties general")
        Set dRng = Ws.Range("G3:G7")
        For Each Rng In dRng
            For Each Sht In Sheets
                If Sht.Name = Rng.Value Then
                    Sht.Visible = xlSheetHidden
                End If
            Next Sht
        Next Rng
        
    
    End Sub
    Sub Un_hide_properties()
    
        Dim Ws As Worksheet, Sht As Worksheet
        Dim dRng As Range, Rng As Range
        
        Set Ws = Worksheets("properties general")
        Set dRng = Ws.Range("G3:G7")
        For Each Rng In dRng
            For Each Sht In Sheets
                If Sht.Name = Rng.Value Then
                    Sht.Visible = xlSheetVisible
                End If
            Next Sht
        Next Rng
    
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Contributor
    Join Date
    02-08-2012
    Location
    South Suffolk
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: Hide/unhide sheets as per list in named range

    If you have sheet names in a range, you can just loop through all the names (either by each cell or using an offset such as range_name(x,1)). It is then just a matter of setting the worksheet visible property
    worksheets(sName).Visible = xlSheetHidden 'where sName is the sheet name
    I often put in a hidden sheet listing all the worksheets along with visibility, protection, cell selection, scroll area. On start-up the code checks the sheets are present and sets the values per the list. Other code can unhide, remove the protection etc. This is good for debugging.
    At least one sheet must be visible but I don't think you have that problem.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide/unhide sheets as per list in named range

    Do you really need to have the list on a sheet. Someone could delete that list or a sheet name in it & the code would.t work.

    You could simply have the list within the code
    Option Explicit
    
    Sub HideEm()
        Dim ws As Worksheet
    
        For Each ws In ThisWorkbook.Worksheets
            Select Case ws.Name
            Case "Interface", "Properties M60", " Properties M61", "Properties M62", _
                 "Properties M64", "Properties general"
                ws.Hidden = xlSheetVeryHidden
            Case Else
            End Select
        Next ws
    End Sub
    Hope that helps.

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

    Free DataBaseForm example

  5. #5
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Hide/unhide sheets as per list in named range

    Thanks for all of the answers. I went with solution provided by Mordred.
    I just changed Ws.Range("G3:G7") to Ws.Range("Sheet_count_DYNAMIC_Properties"). Its just a matter of taste.

    Roy's solution would work as well, but I would like to avoid hard coding it in on this occasion.


    ExcelForums.com never fails. Thanks again.

+ 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