Hi all, I have the following sub that populates a WS called 'Contents' with all the sheets in the workbook. Whilst testing with the end user they asked if the sheets listed could be sorted from oldest to newest.
The current and subsequent sheets in the workbook will all be in the format of '01-01-24 - Name1' (any new sheet names are created dependant on what userform is selected will always be in that format)
Could anyone let me know if its possible to adapt this code below to include the sort function?
Many thanks Paul
The current and subsequent sheets in the workbook will all be in the format of '01-01-24 - Name1' (any new sheet names are created dependant on what userform is selected will always be in that format)
Could anyone let me know if its possible to adapt this code below to include the sort function?
Many thanks Paul
VBA Code:
Sub Contents()
Const showHidden As Boolean = False
Dim wsList As Worksheet
Dim sheetIndex As Integer
Dim rowNumber As Integer
Dim colNumber As Integer
Dim sheetCounter As Integer
On Error Resume Next
Set wsList = ThisWorkbook.Sheets("Contents")
On Error GoTo 0
If wsList Is Nothing Then
Set wsList = ThisWorkbook.Sheets.Add(Before:=ThisWorkbook.Sheets(2))
wsList.Name = "Contents"
Rows("2").RowHeight = 25
Rows("3").RowHeight = 5
Rows("5:500").RowHeight = 18
Columns("B").ColumnWidth = 70
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
Else
wsList.Cells.Clear
End If
With wsList.Range("B2")
.Value = "Contents"
.Font.Bold = True
.Font.Color = RGB(21, 96, 130)
.Font.Name = "Calibri"
.Font.Size = 20
.Font.Underline = True
.HorizontalAlignment = xlCenter
End With
With wsList.Range("B4")
.Value = "Click once on the link below of the event you want to view."
.Font.Bold = True
.Font.Color = RGB(21, 96, 130)
.Font.Name = "Calibri"
.Font.Size = 12
.Font.Italic = True
.HorizontalAlignment = xlCenter
End With
rowNumber = 6
colNumber = 2
For sheetIndex = 1 To ThisWorkbook.Sheets.Count
With ThisWorkbook.Sheets(sheetIndex)
If .Name <> "Contents" And _
(showHidden Or .Visible = xlSheetVisible) Then
wsList.Hyperlinks.Add _
Anchor:=wsList.Cells(rowNumber, colNumber), _
Address:="", _
SubAddress:="'" & _
ThisWorkbook.Sheets(sheetIndex).Name & _
"'!A1", _
TextToDisplay:=ThisWorkbook.Sheets(sheetIndex).Name
rowNumber = rowNumber + 1
End If
End With
Next sheetIndex
End Sub