Consulting

Results 1 to 7 of 7

Thread: TABLE OF CONTENTS NOT USING TABS

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location

    TABLE OF CONTENTS NOT USING TABS

    I want to create a table of contents based not on the tab names, but instead the data in a specific cell (B1 - which is the same for all the sheets) as it contains a more detailed description of the sheet. The tab names are abbreviations which would be meaningless to most users.

    So far I have found everything I need to create this table of contents except how to loop through all the sheets and get the description in B1 into the TOC.

    Any advice appreciated.

  2. #2
    Knowledge Base Approver VBAX Guru
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    4,669
    For Each sht In ThisWorkbook.Worksheets
      MsgBox sht.Range("B1").Value
    Next sht
    p45cal - - - - -This is my signature, it appears after all my posts. Below is not directed at anyone in particular - so don't take offence! - (You might guess why it's there though)
    If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    or this way
    Dim ws as Worksheet
    Dim s as integer
    For s = 1 To ThisWorkbook.sheets.Count  'Actual data starts at sheet ?
            sheets(s).Activate
            'MsgBox "The name of the active sheet is " & ActiveSheet.Name
    Set ws = Worksheets(ActiveSheet.Name)
            With ws
               'Your code here
            End with
    Next s
    

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,819
    Location
    Easy enough to add a hyperlink to go to the sheet. The link text is the B1 for each sheet. I added the ws name in col B, but not needed or you could add more information

    Capture.JPG


    Option Explicit
    Sub MakeTOC()
        Dim wsTOC As Worksheet, ws As Worksheet
        Dim iTOC As Long
        
        
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("Table of Contents").Delete
        Application.DisplayAlerts = False
        On Error GoTo 0
        
        Worksheets.Add Before:=Worksheets(1)
        ActiveSheet.Name = "Table of Contents"
        Set wsTOC = Worksheets("Table of Contents")
        
        iTOC = 1
        
        For Each ws In Worksheets
            If Not ws Is wsTOC Then
                wsTOC.Hyperlinks.Add Anchor:=wsTOC.Cells(iTOC, 1), Address:="", SubAddress:=ws.Name & "!A1", TextToDisplay:="=" & ws.Name & "!B1"
                wsTOC.Cells(iTOC, 2).Value = ws.Name
                iTOC = iTOC + 1
            End If
        Next
        wsTOC.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
    End Sub
    Attached Files Attached Files
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    275
    Location
    It's nice to have a return link too.

    For Each ws In Worksheets
            If Not ws Is wsTOC Then
                wsTOC.Hyperlinks.Add Anchor:=wsTOC.Cells(iTOC, 1), Address:="", SubAddress:=ws.Name & "!A1", TextToDisplay:="=" & ws.Name & "!B1"
                ws.Hyperlinks.Add Anchor:=ws.Cells(1, 1), Address:="", SubAddress:="'Table of Contents'!A1", TextToDisplay:="Back" 'Return Link
                wsTOC.Cells(iTOC, 2).Value = ws.Name
                iTOC = iTOC + 1
            End If
        Next
    "To a man with a hammer everything looks like a nail." - Mark Twain

  6. #6
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,819
    Location
    Quote Originally Posted by david000 View Post
    It's nice to have a return link too.
    Yea -- I like that
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    Thank you all for your help! Much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •