Insert Tab name into worksheet?

scewing

New Member
Joined
Nov 4, 2002
Messages
3
Other than using headers and footers, is there a way to insert the name of a tab into a cell on a worksheet? I know you can insert the filename into a cell using the CELL function, but I can't seem to find a way to insert the tab name into a cell. Thanks in advance!
 
You can use Zack Barresse's Table of Contents generator:

Sub CreateTOC()
****'** Code by Zack Baresse
****If ActiveWorkbook Is Nothing Then
********MsgBox "You must have a workbook open first!", vbInformation, "No Open Book"
********Exit Sub
****End If
****
****With Application
********.ScreenUpdating = False
********.DisplayAlerts = False
****
********Dim ws As Worksheet, _
************ct As Chart, _
************shtName As String, _
************nrow As Long, _
************tmpCount As Long, _
************i As Long, _
************numCharts As Long
********
********nrow = 3
********i = 1
********numCharts = ActiveWorkbook.Charts.Count
********
********On Error GoTo hasSheet
********Sheets("Table of Contents").Activate
********If MsgBox("You already have a Table of Contents page.**Would you like to overwrite it?", _
********vbYesNo + vbQuestion, "Replace TOC page?") = vbYes Then GoTo createNew
********Exit Sub

hasSheet:
****Sheets.Add Before:=Sheets(1)
****GoTo hasNew

createNew:
****Sheets("Table of Contents").Delete
****GoTo hasSheet

hasNew:
****tmpCount = ActiveWorkbook.Charts.Count
****If tmpCount > 0 Then tmpCount = 1
********ActiveSheet.Name = "Table of Contents"
********
********With Sheets("Table of Contents")
************'.Cells.Interior.ColorIndex = 4
****************With .Range("B2")
********************.Value = "Table of Contents"
********************.Font.Bold = True
********************.Font.Name = "Calibri"
********************.Font.Size = "24"
****************End With
********End With
********
********For Each ws In ActiveWorkbook.Worksheets
************nrow = nrow + 1
************With ws
****************shtName = ws.Name
****************With Sheets("Table of Contents")
********************.Range("B" & nrow).Value = nrow - 3
********************.Range("C" & nrow).Hyperlinks.Add _
************************Anchor:=Sheets("Table of Contents").Range("C" & nrow), Address:="#'" & _
************************shtName & "'!A1", TextToDisplay:=shtName
********************.Range("C" & nrow).HorizontalAlignment = xlLeft
****************End With
************End With
********Next ws
********
********If numCharts <> 0 Then
************For Each ct In ActiveWorkbook.Charts
****************nrow = nrow + 1
****************shtName = ct.Name
****************With Sheets("Table of Contents")
********************.Range("B" & nrow).Value = nrow - 3
********************.Range("C" & nrow).Value = shtName
********************.Range("C" & nrow).HorizontalAlignment = xlLeft
****************End With
************Next ct
********End If
********
********With Sheets("Table of Contents")
************With .Range("B2:G2")
****************.MergeCells = True
****************.HorizontalAlignment = xlLeft
************End With
********
************With .Range("C:C")
****************.EntireColumn.AutoFit
****************.Activate
************End With
************.Range("B4").Select
********End With
****
********.DisplayAlerts = True
********.ScreenUpdating = True
****End With
****
****MsgBox "Done!" & vbNewLine & vbNewLine & "Please note: " & _
********"Charts are listed after regular " & vbCrLf & _
********"worksheets and will not have hyperlinks.", vbInformation, "Complete!"

End Sub


HTH,

Hi Guys,

Apologies for gatecrashing a super thread, can i ask an additional question to the above code please.

I have a workbook where i have around 25 No worksheets and when i use the above code it neatly gives a table of contents as described.

My problem is if i rename any of the worksheet name tabs the hyperlink (in the Table of Contents) to that sheet breaks down.......a pop up window says (Reference is not valid).

Is this an easy solution?

I am new to excel and have no VBA skills whatsoever, can somebody help.

Thanks in advance.

Paul J
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the Board!

If you rename a sheet you simply rerun the code.

HTH,

Hi Smitty

Thanks for the welcome and quick response!

I did what you suggested and it worked, however it deleted stuff I'd copied across from my index page to the new table of contents page.

I already have an index page with a table of contents but my original problem (my index page) is if i change the worksheet tab name it breaks the hyperlink hence why I'd copied your code thinking that would solve my problem.

Again i apologize for me being illiterate with excel but I've only been using it more frequently for a little over 4 months, any suggestion(s) would be much appreciated.

Again thanks in advance.

Paul J.
 
Upvote 0
If you already have an existing Index sheet, then you'll need to manually adjust the hyperlinks.

As why anything that you may have copied to the newly created Table of Contents gets deleted, it's because the TOC sheet is first deleted if it already exists, then recreated from scratch.
 
Upvote 0
If you already have an existing Index sheet, then you'll need to manually adjust the hyperlinks.

As why anything that you may have copied to the newly created Table of Contents gets deleted, it's because the TOC sheet is first deleted if it already exists, then recreated from scratch.

That's what I'm doing already.

Never mind, thanks for looking and your time, Cheers.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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