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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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