TOC sorted alphabetically

Victtor

Board Regular
Joined
Jan 4, 2007
Messages
170
Office Version
  1. 365
Platform
  1. Windows
I have code to create a Table of Contents based on worksheets. Can someone please add code to have this list sorted alphabetically? Thanks in advance

Code:
Sub tocmaker()
Dim wsh As Worksheet
Dim cnt As Long
Dim doit As String
If Application.CountA(ActiveSheet.Range("A:A")) > 0 Then
doit = MsgBox("There is already text in column A of " & ActiveSheet.Name & " where the TOC will be made. This will overwrite data in column A. Do it anyway?", vbYesNo, "ALERT")
If doit <> vbYes Then
Exit Sub
End If
End If
'make toc
ActiveSheet.Range("A1") = "TABLE OF CONTENTS:"
cnt = 2
For Each wsh In ActiveWorkbook.Worksheets
If wsh.Visible = True And wsh.Name <> ActiveSheet.Name Then
ActiveSheet.Cells(cnt, 1) = wsh.Name
ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(cnt, 1), Address:="", SubAddress:="'" & wsh.Name & "'!A1", TextToDisplay:=wsh.Name
cnt = cnt + 1
End If
Next
MsgBox "Table of Contents created on " & ActiveSheet.Name & vbCr & "Use MAIN MENU creator to make a return link to main menu."
End Sub
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Just add this to the end of your code
Code:
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveSheet.Sort
        .SetRange Range("A1", Range("A" & Rows.Count).End(xlUp))
        .Header = xlYes
        .MatchCase = True
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top