+ Reply to Thread
Results 1 to 8 of 8

Exclude hidden sheets in Table of Contents code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-20-2021
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    21

    Exclude hidden sheets in Table of Contents code

    Hello, I'm using the following code to generate a TOC. Is there a way to make it exclude any hidden sheets?
    Many thanks.

    Sub CreateLinksToAllSheets()
    Dim sh As Worksheet
    Dim cell As Range
    Range("C7:C40").Select
    Selection.ClearContents
    Range("C7").Select
    For Each sh In ActiveWorkbook.Worksheets
        If ActiveSheet.Name <> sh.Name Then
            ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
            "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
            ActiveCell.Offset(1, 0).Select
        End If
    Next sh
        Range("C7:C40").Select
        With Selection.Font
            .Size = 13
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
    Range("o4").Select
    End Sub
    Last edited by MaggieLou; 01-21-2021 at 12:18 PM.

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,498

    Re: Exclude hidden sheets in Table of Contents code

    Yes, quite easily in fact.
    But first you need to comply with the forum rules and use code tags around the code in your post.

    BSB

  3. #3
    Registered User
    Join Date
    01-20-2021
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    21

    Re: Exclude hidden sheets in Table of Contents code

    sorry, 2nd day on site. Is this correct?

    Sub CreateLinksToAllSheets()
    Dim sh As Worksheet
    Dim cell As Range
    Range("C7:C40").Select
    Selection.ClearContents
    Range("C7").Select
    For Each sh In ActiveWorkbook.Worksheets
    If ActiveSheet.Name <> sh.Name Then
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
    ActiveCell.Offset(1, 0).Select
    End If
    Next sh
    Range("C7:C40").Select
    With Selection.Font
    .Size = 13
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With
    Range("o4").Select
    End Sub

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,498

    Re: Exclude hidden sheets in Table of Contents code

    I kinda meant put the code tags in your original post....

    Add the lines in red to your code and give that a whirl.

    Sub CreateLinksToAllSheets()
        Dim sh As Worksheet
        Dim cell As Range
        Range("C7:C40").ClearContents
        Range("C7").Select
        For Each sh In ActiveWorkbook.Worksheets
            If sh.Visible = True Then
                If ActiveSheet.Name <> sh.Name Then
                    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
                    "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
                    ActiveCell.Offset(1, 0).Select
                End If
            End If
        Next sh
        Range("C7:C40").Select
        With Selection.Font
            .Size = 13
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        Range("o4").Select
    End Sub
    BSB

  5. #5
    Registered User
    Join Date
    01-20-2021
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    21

    Re: Exclude hidden sheets in Table of Contents code

    Got it, thanks for the guidance.
    And thanks for the code - works perfectly.

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,498

    Re: Exclude hidden sheets in Table of Contents code

    Happy to help.

    Worth pointing out that you rarely need to select a range to manipulate it through VBA.
    So this from your code:
        Range("C7:C40").Select
        With Selection.Font
    Could just as easily be:
        With Range("C7:C40").Font
    I'd already changed the ClearContents part near the start of your code but didn't spot this one until after I replied.

    BSB

  7. #7
    Registered User
    Join Date
    01-20-2021
    Location
    Toronto, Canada
    MS-Off Ver
    2013
    Posts
    21

    Re: Exclude hidden sheets in Table of Contents code

    thanks again! I'm such a VBA newbie - I really appreciate it!

  8. #8
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,498

    Re: Exclude hidden sheets in Table of Contents code

    No problem. We're here to help. We were all VBA noobs at some point :D

    BSB

+ 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. Table of Contents | How i can create it without hidden Sheets?
    By pacosalasv in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-22-2020, 12:40 PM
  2. VBA Code to exclude sheets with no data
    By angelmaine4ever in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-06-2017, 05:32 AM
  3. [SOLVED] Help with code to open hidden sheets based on contents in a cell.
    By holli in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-04-2016, 10:06 PM
  4. [SOLVED] Exclude Hidden Sheets/"Home" Sheet when creating hyperlinks
    By Kenny Blackwell in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-30-2015, 12:36 PM
  5. How to exclude hidden rows while running a code?
    By heena.singh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2013, 06:25 AM
  6. Need to change code that loops all sheets TO exclude some sheets.
    By sherrie777 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 04-10-2010, 05:29 PM
  7. [SOLVED] Edit code - exclude sheets
    By Steph in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-15-2006, 11:40 AM

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