what if i dont want to clear the contents of the Index sheet every time as my workbook contains lot many number of sheets. Each time deleting all the contents and recreating them may take time. I just want to append the newly created sheets to the existing table of contents.
Actually i tried making the following changes in the code. it was woking but when ever the new sheets are careted, the old hyperlinks are getting disappeared and hyperlinks for the new sheets getting activated.
Can you please suggest on this?
Sub IDX()
Dim ws As Worksheet, i As Integer
If WorksheetExists("Index") Then
' Worksheets("Index").UsedRange.ClearContents //i commented this line
i = Worksheets("Index").UsedRange.Rows.Count // i added this line
Else
Worksheets.Add(before:=Worksheets(1)).Name = "Index"
End If
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Index" Then
i = i + 1
Sheets("Index").Range("A" & i).Value = ws.Name
Sheets("Index").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
End If
Next ws
Sheets("Index").Columns("A").AutoFit
End Sub
Try
Code:
Sub IDX()
Dim ws As Worksheet, i As Integer
If WorksheetExists("Index") Then
Worksheets("Index").UsedRange.ClearContents
Else
Worksheets.Add(before:=Worksheets(1)).Name = "Index"
End If
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Index" Then
i = i + 1
Sheets("Index").Range("A" & i).Value = ws.Name
Sheets("Index").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
End If
Next ws
Sheets("Index").Columns("A").AutoFit
End Sub
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function