Sub CreateIndexSheetAndAddLinks()
Dim ws As Worksheet
Dim indexSheet As Worksheet
Dim i As Integer
Dim lastRow As Long
' Ki?m tra xem sheet "Index" dã t?n t?i chua
On Error Resume Next
Set indexSheet = ThisWorkbook.Sheets("Index")
On Error GoTo 0
If Not indexSheet Is Nothing Then
' Xóa sheet "Index" n?u dã t?n t?i
Application.DisplayAlerts = False
indexSheet.Delete
Application.DisplayAlerts = True
End If
' T?o sheet "Index" m?i
Set indexSheet = ThisWorkbook.Sheets.Add
indexSheet.Name = "Index"
' Thi?t l?p tiêu d?
indexSheet.Cells(1, 1).Value = "Sheet Name"
indexSheet.Cells(1, 2).Value = "Link"
' Li?t kê tên các sheet và t?o liên k?t
i = 2
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "Index" Then
indexSheet.Cells(i, 1).Value = ws.Name
indexSheet.Hyperlinks.Add Anchor:=indexSheet.Cells(i, 2), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:="Go to " & ws.Name
' Thêm liên k?t "Back to Index" vào ô A1 c?a m?i sheet
ws.Hyperlinks.Add Anchor:=ws.Cells(1, 1), Address:="", SubAddress:="Index!A1", TextToDisplay:="Back to Index"
i = i + 1
End If
Next ws
' Ð?nh d?ng b?ng
With indexSheet.Cells(1, 1).CurrentRegion
.Borders.LineStyle = 1
.EntireColumn.AutoFit
End With
End Sub