Introduction
When managing multiple Excel files for work, you often encounter situations where you want to open another workbook directly from a specific cell.
This article introduces how to use VBA to automatically insert hyperlinks to other Excel workbooks.
Intended Use Cases
- Opening another workbook with a single click.
- Automating the link insertion process to reduce manual work.
- Using relative paths so links do not break even if files are moved.
VBA Code Example
The following code inserts a link into cell C3 that opens a file named “SummaryResult.xlsx” located in the same folder as the current workbook.
Sub InsertLinkToAnotherWorkbook()
Dim linkTargetPath As String
Dim linkCell As Range
' Specify the path of the target workbook using a relative path
' ThisWorkbook.Path gets the folder of the current file
linkTargetPath = ThisWorkbook.Path & "\SummaryResult.xlsx"
' The cell where the hyperlink will be inserted
Set linkCell = ActiveSheet.Range("C3")
' Insert the hyperlink
ActiveSheet.Hyperlinks.Add _
Anchor:=linkCell, _
Address:=linkTargetPath, _
TextToDisplay:="Open Summary File"
End Sub
Code Explanation
| Element | Description |
ThisWorkbook.Path | Retrieves the folder path where the current workbook is saved. |
& "\SummaryResult.xlsx" | Specifies the target file name within the same folder (Relative Path). |
Hyperlinks.Add | The standard VBA method to add a hyperlink to a specified range. |
What if you want to use an Absolute Path?
If you need to specify the full path explicitly (Absolute Path), write the address like this:
Address:="C:\Users\Username\Documents\SummaryResult.xlsx"
Benefits of Using Relative Paths
- No Broken Links: The link remains valid even if you move the entire folder to a different location.
- Easy Sharing: It works correctly when sent to other team members, as it doesn’t rely on a specific user’s C: drive path.
- Cloud Compatibility: It adapts flexibly to OneDrive or shared network drives.
Summary
With Excel VBA, you can easily insert hyperlinks to other Excel files.
Using ThisWorkbook.Path to set relative paths significantly improves file maintainability and makes team collaboration smoother.
Use this method to free yourself from the hassle of manually inserting links and the worry of broken connections.
