Consulting

Results 1 to 5 of 5

Thread: Table of content

  1. #1
    VBAX Newbie
    Joined
    May 2013
    Posts
    2
    Location

    Table of content

    I tried using one of the knowledge base articles vbaexpress.com/kb/getarticle.php?kb_id=16 to create a table of content. It worked fine except for one problem, the TOC always left out the last sheet in the workbook.
    I went through the code and couldn't find anything limiting the number of sheets. However, I am very new at using VBA so I was hoping someone could tell me what was wrong.

    Thanks in advance.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Is the last sheet a chart sheet? That code won't do Chart Sheets.

    This is the code for the index sheet I use. I can't always find a common empty cell to put a hyperlink back to the index, So this code keeps the index Sheet next to the sheet selected from it's list.

    [VBA]Option Explicit

    Private Sub Worksheet_Activate()
    'An interactive index that refreshes each time
    'it's viewed so that it is always current, even if
    'Sheets are added, deleted, or renamed.
    '
    'The Selection Change Sub below keeps this sheet next to
    'the Sheet chosen when the User clicks on a Sheet name
    'in the Sheet Index List.


    'ShtNdx is the CodeName of "Sheet Index"
    Const TopRowOfList As Long = 4 'Set As Desired
    Dim i As Long 'Common index variable
    Dim r As Long 'Row Counter for Index List
    r = TopRowOfList 'Set here because it's used to clear the list

    Application.ScreenUpdating = False

    ''''Clear the existing list
    If LastRow >= TopRowOfList Then ShtNdx.Range("A" & CStr(TopRowOfList) & _
    ":A" & CStr(LastRow)).ClearContents

    ''''Create the List of Sheet Names
    With ThisWorkbook
    For i = 1 To .Sheets.Count
    If .Sheets(i) Is ShtNdx Then
    'Don't list This sheet
    'Don't increment Row counter
    GoTo NextLoop
    Else
    ShtNdx.Cells(r, 1) = .Sheets(i).Name
    r = r + 1 'Increment Row Counter
    End If
    NextLoop:
    Next i
    End With

    ''''Sort the list alphabetically
    Range("A4:A" & CStr(LastRow)).Sort _
    Key1:=Range("A1"), _
    Header:=xlNo

    Application.ScreenUpdating = True
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.ScreenUpdating = False

    ''''Check criteria for running this sub
    'Is only one Cell selected
    If Target.Count <> 1 Then Exit Sub
    'Is there a List
    If LastRow < 4 Then Exit Sub 'TopRowOfList
    'Is the selection in the List
    If Intersect(Target, Range("A4:A" & CStr(LastRow))) Is Nothing Then Exit Sub


    ''''Move the Index Sheet, Activate the chosen Sheet, and Select "A1" _
    on the chosen Sheet.
    ShtNdx.Move Before:=Sheets(Target.Value)
    Sheets(Target.Value).Activate
    ActiveSheet.Range("A1").Select

    Application.ScreenUpdating = True
    End Sub

    Private Function LastRow() As Long
    'Custom for this module.
    'Always looks for the last non-empty cell in Column "A."
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    End Function[/VBA]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    On reviewing that code, I realized that I had only designed it for my own use.

    YOu must make sure that whatever Tab name you give the sheet, it's CodeName must be ShtNdx.

    To make it Universal Move this line to above the first sub.
    [VBA]Const TopRowOfList As Long = 4 'Set As Desired[/VBA]

    Recode the "4" in these lines to use TopRowOfList
    [VBA]If LastRow < 4 Then Exit Sub 'TopRowOfList
    'Is the selection in the List
    If Intersect(Target, Range("A4:A" & CStr(LastRow))) Is Nothing Then Exit Sub[/VBA]

    Or, If you don't mind it being having hardcoded values and you really don't want the list starting at Row 4, just change all the values "4" to whichever row you want.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Newbie
    Joined
    May 2013
    Posts
    2
    Location

    Not a chart sheet

    The last sheet is not a chart sheet. All the sheets contain the same format and formulas, just the input changes. I found another code to do what I wanted. I was just wondering why the first code kept skipping the last sheet.

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    the procedure in the KB article ( http://www.vbaexpress.com/kb/getarticle.php?kb_id=16 ) assumes active worksheet is the "Index" worksheet and active cell is the starting cell for hyperlinks to other worksheets.

    if you are active in a worksheet other than "Index", this worksheet will be excluded from list.

    related lines of the procedure:
    [vba]Set WsInd = ActiveSheet[/vba]

    [vba]If Ws.Name <> WsInd.Name Then[/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    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
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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