Macro creates links to all sheets, tables, pivot tables and named ranges
This article demonstrates a macro that automatically populates a worksheet with a Table of Contents, it contains hyperlinks to worksheets, Pivot tables, Excel defined Tables and named ranges.
The hyperlinks allow you to navigate quickly to any of the Excel objects mentioned above.
What you will learn in this article
- Iterate through worksheets in the active workbook.
- Iterate through Pivot Tables in the active worksheet.
- Iterate through Excel defined Tables in the active worksheet.
- Iterate through named ranges in the active workbook.
- Create hyperlinks programmatically.
- Change column width so the content fits.
- Save text value to a cell using VBA.
- Bold cell text programmatically.
- Select the next cell below using VBA.
How to use macro
The animated image above demonstrates the macro.
- Select a destination cell on a worksheet you want to populate.
- Go to tab "Developer" on the ribbon.
- Press with left mouse button on the "Macros" button and the Macro dialog box appears.
- Select macro CreateToC.
- Press with left mouse button on "Run" button.
- The macro creates hyperlinks to all worksheets, pivot tables, Excel defined Tables and named ranges in the active workbook.
- The macro ends.
VBA code
'Name macro Sub CreateToC() 'Dimension variables and declare data types Dim sh As Worksheet Dim cell As Range Dim pt As PivotTable Dim tbl As ListObject Dim nms As Name 'Populate selected cell with "Table of Contents" ActiveCell.Value = "Table of Contents" 'MAke the selected cell bolded ActiveCell.Font.Bold = True 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Save text Worksheets to selected cell ActiveCell.Value = "Worksheets" 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Iterate through each worksheet in active workbook For Each sh In ActiveWorkbook.Worksheets 'Make sure the worksheet name is not equal to the currently selected worksheet If ActiveSheet.Name <> sh.Name Then 'Add hyperlink to selected cell with worksheet name linking the the worksheet ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _ "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name 'Select the next cell below ActiveCell.Offset(1, 0).Select End If Next sh 'Save text Pivot tables to selected cell ActiveCell.Value = "Pivot tables" 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Iterate through all worksheets in active workbook For Each sh In ActiveWorkbook.Worksheets 'Iterate through all pivot tables in worksheet For Each pt In sh.PivotTables 'Add hyperlink to selected cell with pivot table name linking to the pivot table ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _ "'" & sh.Name & "'!" & pt.TableRange1.Address, TextToDisplay:=pt.Name 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Continue with next pivot table Next pt 'Continue with next worksheet Next sh 'Save text Tables to selected cell ActiveCell.Value = "Tables" 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Iterate through all worksheets in active workbook For Each sh In ActiveWorkbook.Worksheets 'Iterate through all Excel defined Tables in worksheet For Each tbl In sh.ListObjects 'Add hyperlink to selected cell with Excel defined Table name linking to the Excel defined Table ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _ tbl.Name, TextToDisplay:=sh.Name 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Continue with next Excel defined Table Next tbl 'Continue with next worksheet Next sh 'Save text Named Ranges to selected cell ActiveCell.Value = "Named ranges" 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Iterate through all named ranges in workbook For Each nms In ActiveWorkbook.Names 'Add hyperlink to selected cell with the name of the named range linking to the the named range ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _ nms.Name, TextToDisplay:=nms.Name 'Select the next cell below ActiveCell.Offset(1, 0).Select 'Continue with next named range Next nms 'Change column widths so they fit the content ActiveSheet.Columns(ActiveCell.Column).AutoFit End Sub
Where to put the code?
- Copy above VBA code.
- Press Alt + F11 to open the Visual Basic Editor.
- Select your workbook in the Project Explorer.
- Press with left mouse button on "Insert" on the menu.
- Press with left mouse button on "Module" to insert a code module.
- Paste VBA code to the code module.
- Return to Excel.
Hyperlinks category
The macro demonstrated above creates hyperlinks to all worksheets in the current worksheet. You will then be able to quickly […]
Sean asks: Basically, when I do a refresh of the data in the "pivotdata" worksheet, I need it to recognise […]
Today I'll show you a formula that returns a hyperlink pointing to a location based on a lookup value. When […]
Named range category
A dynamic named range grows automatically when new values are added and also shrinks if values are deleted. This saves […]
This article demonstrates how to populate a drop down list with filtered values from an Excel defined Table. The animated […]
This article shows you a way to display all named ranges you have in a workbook. This is a powerful […]
Excel categories
3 Responses to “Macro creates links to all sheets, tables, pivot tables and named ranges”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Sub Cmdty()
Dim PT As PivotTable
Dim PTcache As PivotCache
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next
Sheets.Add
ActiveSheet.Name = "PVT"
Set PTcache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range("A5,DC6577"))
Set PT = ActiveSheet.PivotTables.Add(PTcache, Range("A1"), "Table")
With PT
.AddFields("GROUP").Orientation = xlRowField
.AddFields("QTY").Orientation = xlDataField
.AddFields("STD").Orientation = xlDataField
.AddFields("MR$").Orientation = xlDataField
.AddFields("FCST").Orientation = xlDataField
.AddFields("PPV").Orientation = xlDataField
End With
End Sub
actually I corrected the code:
...
Sheets("PVT").Select
Set PT = ActiveSheet.PivotTables.Add(PTcache, Range("A1"), "Table")
...
Still to no avail... if you will notice my data range is very long... It's very frustrating I cannot work it out on myself. could you help out on this? Thanks in advance
[…] Welcome to the forum, Take a look at this link it may help find a solution for you. Quickly create links to sheets, tables, pivot tables and named ranges in a workbook | Get Digital He… […]