Written by Allen Wyatt (last updated February 25, 2021)
This tip applies to Excel 2007, 2010, 2013, and 2016
If you have a very large number of worksheets in a workbook, you might want to retrieve the names of those worksheets and put then on their own worksheet. For instance, you may want them in one place so you can use them in a table of contents or in some other fashion. The following macro, GetSheets, will quickly retrieve the names of the worksheets in the current workbook and put them in the current workbook, beginning at whatever cell is currently selected.
Sub GetSheets() Dim w As Worksheet Dim iRow As Integer Dim iCol As Integer iRow = Selection.Row iCol = Selection.Column For Each w in Worksheets Cells(iRow, iCol) = w.Name iRow = iRow + 1 Next w End Sub
If you want to make an actual table of contents where the sheet names are actually hyperlinks to the worksheets, you could modify the macro in the following manner:
Sub MakeTOC() Dim w As Worksheet Dim iRow As Integer Dim iCol As Integer Dim sTemp As String iRow = Selection.Row iCol = Selection.Column For Each w in Worksheets Cells(iRow, iCol) = w.Name sTemp = "'" & w.Name & "'!A1" ActiveSheet.Hyperlinks.Add Anchor:=Cells(iRow, iCol), _ Address:="", SubAddress:=sTemp, TextToDisplay:=w.Name iRow = iRow + 1 Next w End Sub
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11679) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Retrieving Worksheet Names.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
If you have multiple worksheets that each provide different ways to arrive at the same results, you may be wondering how ...
Discover MoreIf you need to work on two worksheets in the same workbook at the same time, Excel makes this rather easy to do. All you ...
Discover MoreFreezing the top rows in a worksheet so that they are always visible is easy to do. Freezing the bottom rows is not so ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-01-23 10:47:30
Dave
Mary Beth,
Allen's instructions as you noted are actually VBA code. They are what you would use to write a macro to do the task for you.
If you are relatively new to Excel, or if you are a light Excel user, then you can and should probably ignore tips that look like these.
On the other hand, if you are or are becoming a serious Excel user, then learning how to write macros can give you many more tools than you ever thought possible, particularly with repetitive tasks.
Learning to write macros is not that hard (I'm self-taught), but it requires a small base of core knowledge. Once acquired, you'd be good to learn to do -- and actually do -- anything you want. Fortunately, there are many resources available online and in books to get you over that initial hump. Excel VBA Programming for Dummies by John Walkenbach comes to mind.
Good luck!
2017-01-23 09:41:41
Mary Beth
Are these your directions?
Sub MakeTOC()
Dim w As Worksheet
Dim iRow As Integer
Dim iCol As Integer
Dim sTemp As String
I do not understand your directions. They make no since.
2017-01-21 04:57:43
Najm
Dear brother,
You really help people do better.
Thanks sharing....
Najm
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments