- How to add Hyperlinks in Excel using VBA Macro
- Tools you can use
- VBA Hyperlinks
- Hyperlinks in Excel VBA
- The formula of VBA Hyperlinks
- How to Create Hyperlinks in Excel VBA?
- Hyperlinks of Multiple Sheets with Loops
- Recommended Articles
- Hyperlinks In Excel– Add Remove Links Formula – Get VBA Code
- How To Add Hyperlinks in Excel Using Formula?
- How To Add Hyperlinks in Excel using VBA?
- How To Read All Hyperlinks in Excel using VBA Macro?
- How to Add hyperlink in Excel Manually?
- How To Delete Hyperlinks in Excel with VBA?
- Create a hyperlink within an Excel cell?
- 6 Answers 6
How to add Hyperlinks in Excel using VBA Macro
Tools you can use
Here is a scenario. I have added a small amount of data on Sheet1 showing annual sales for different regions for the First year of business. The figures are not real. There is another column with header Second Year . I will add figures for every region.
The program (macro) checks if the figure in the Second year is less than the First year. If the amount is less, I want the macro to add a link next to the figure and add another link in Sheet2 linking to the figure in Sheet1 (with less amount).
The first method will add a Hyperlink (in the same sheet) if the entered amount is less than the previous year’s amount.
The second method adds a Hyperlink in Sheet2 (first column). It does have an Address, but a SubAddress linking back to the cell with less amount in Sheet1 . (See picture above)
I have used this method twice in my example. Therefore, it is important that I explain the method and its properties.
The Hyperlinks.Add() method will add a hyperlink to a specified range and it takes five parameters.
Add(Anchor As Object, Address As String, [SubAddress], [ScreenTip], [TextToDisplay]) as Object
The first two parameters are required and the rest are optional.
Anchor (required) – An excel Range to add the anchor for the Hyperlink
Address (required) – The address value of the Hyperlink
SubAddress (optional)– SubAddress property of the Hyperlink
ScreenTip (optional) – Looks similar to a Tooltip. Shows a little message when we hover the mouse over the Hyperlinks.
TextToDisplay (optional) – The Hyperlink label showing an underlined text for users to click the link
Well, now you know how to add Hyperlinks to your Excel sheet, dynamically. This little piece of code will spare you from adding hyperlinks manually to each cell and in the process save precious time. You do not have to worry about missing links anymore.
Hyperlinks are URL attached to a value that one may see when we hover the mouse over it. When we click on it, the URL opens. In VBA, we have an inbuilt property to create hyperlinks in VBA. To use this property, we must use the Add method together with the hyperlink statement to insert a hyperlink in a cell.
Hyperlinks in Excel VBA
Even though we have Page Up and Page Down shortcut key in excel Shortcut Key In Excel An Excel shortcut is a technique of performing a manual task in a quicker way. read more to move from one sheet to another. But it becomes complex when we have to move between 10 to more worksheets. It is where the beauty of “Hyperlinks in Excel” comes into the picture. The hyperlink is a predetermined URL that takes you to the respective cell or worksheet as assigned.
We all know how to quickly create hyperlinks in the worksheet to move from one sheet to another. But, of course, you can also go to any other sheet. But in today’s article, we will show you how to create hyperlinks using VBA coding.
Table of contents
You are free to use this image on your website, templates, etc., Please provide us with an attribution link How to Provide Attribution? Article Link to be Hyperlinked
Source: VBA Hyperlinks (wallstreetmojo.com)
The formula of VBA Hyperlinks
Let us look at the formula of the hyperlinks in Excel VBA.
- Anchor: In which cell would you like to create a hyperlink?
- Address: What is the URL to the hyperlink to navigate?
- [Sub Address]: What is the location of the page?
- [Screen Tip]: What is the value shown when you place a mouse pointer on the hyperlink name or cell?
- [Text to Display]: What test will display in the cell? For example, Worksheet Name.
How to Create Hyperlinks in Excel VBA?
Assume you want to create a VBA hyperlink to the “Main Sheet” sheet from the other sheet, “Example 1.”
Step 1: First, select the cell A1 of the worksheet example 1.
Step 2: Now, open hyperlinks using the Active Cell object. Add method.
Step 3: The first argument is “Anchor,” i.e., in which cell we would link to create the VBA hyperlink. In this case, cell A1 and since we have already selected cell A1 to mention it as “Selection.”
Step 4: We are not creating any address here, so ignore the Address as of now.
Step 5: Next is ‘Sub Address.’ Here, we need to mention which sheet we are referring to and the first cell of that sheet.
We have mentioned the sheet name as “Main Sheet.” In that sheet cell address is “A1.”
Step 6: Ignore the screen tip as well. For text to display, mention the sheet name.
Run this code using the F5 key or manually. Then, it will create a hyperlink in cell A1 in the sheet “Example 1.”
When you click on the hyperlink “Main Sheet,” it redirects to the main sheet.
Hyperlinks of Multiple Sheets with Loops
We have seen creating a VBA hyperlink for one sheet. However, when we have many sheets, it isn’t easy to create a VBA hyperlink for each sheet with the same line of code.
Assume you have 11 worksheets, as shown in the below image.
You want to create a hyperlink for each sheet in the Index sheet using VBA code.
Step 1: Define the variable as a worksheet.
Step 2: The first thing is to select the worksheet Index and select cell A1.
Step 4: Since we have selected cell A1, it is now an active cell. So, start the hyperlink with the active cell.
Step 5: An anchor is a hyperlink cell. So, it is the active cell.
Step 6: Address is nothing mentioned it as “.
Step 7: Subaddress: When we loop through the sheet, it should be the sheet name. To refer to the sheet name, we need a single quote, “’” with sheet name and “! Cell Address,” and close the sheet name with a single quote “’.”
Step 8: Ignore the screen tip. For text to display, you can enter the worksheet name.
Step 9: To store the hyperlink of each sheet in a different cell, every time a hyperlink, one must create it for one sheet. We need to move down one cell from the active cell.
It will create a hyperlink of all the sheets in the Index sheet. This code is dynamic whenever there is any addition or deletion of sheets. Therefore, we need to run this code to have an updated hyperlink.
This article has been a guide to VBA Hyperlinks. Here, we learn how to create hyperlinks in the worksheet using VBA code to quickly move from one sheet to another, along with some simple to advanced examples. Below are some useful Excel articles related to VBA: –
Hyperlinks In Excel– Add Remove Links Formula – Get VBA Code
How To Add Hyperlinks in Excel Using Formula?
Higher version of MS Excel like 2007 or above have built in function to add links.
- Select a cell to insert a hyperlink.
- Enter “=HyperLink” & enter value for below parameters.
- Type website URL address or destination file path as link.
- Type any text to display as friendly name in Excel.
- Example: =HYPERLINK (“https://officetricks.com”, “Refer Officetricks for Excel Tips”)
- Press Enter.
If not a website url, You can also enter complete path for a file name in the first parameter as hyperlink.
Once this is done, when any use click this cell, the corresponding file or website will be opened in your default browser.
Note: Hyperlinks added using this formula cannot be read using the below VBA codes.
How To Add Hyperlinks in Excel using VBA?
The below code has sample VBA to add website Url & also file path as hyperlink.
Open a new workbook, press Alt+F11 & copy paste this below code. Once you press F5, it will add hyperlinks in active sheet.
Above code will add hyperlinks twice in the same cell. To avoid this, enter a different location in second command. For example: instead of ‘Activesheet.Hyperlinks’ use ‘Thisworkbook.Sheets(1).Cells(1,2).Hyperlinks’
How To Read All Hyperlinks in Excel using VBA Macro?
Use this VBA code to read all the Hyperlinks in Your Excel sheet.
How to Add hyperlink in Excel Manually?
To insert a hyperlink in Excel follow these steps.
- Right click cell.
- Choose “Hyperlink” in menu.
- Type value for “Text to display” field.
- Enter “Address” or choose a path from Existing File or Web page.
- Click ‘Ok’.
This will add a hyperlink in the chosen cell. This cell will display the words you typed in the field “Text to field” with a underline and blue color. Once a user clicks on this cell or link, Excel will open the hyperlink in the default browser or in the desktop.
If the file or field entered is wrong, you will get an error message.
How To Delete Hyperlinks in Excel with VBA?
To manually remove a hyperlink from Excel, right click on the corresponding cell in the worksheet. Then choose ‘Remove hyperlink’ option.
To remove it using the VBA code, use ‘Delete’ instead of ‘.Add’ or ‘.Address’ in the above commands. Once you press F5, the VBA code will remove the hyperlink.
Also, if you want to edit value of a hyperlink, then choose ‘Edit Hyperlink’ option & change the values.
Create a hyperlink within an Excel cell?
Is it possible to create a hyperlink within an Excel cell which only uses a section of the cell text for the clickable link? I.E. would the below table mockup represent something that can be easily built in Excel 2010?
I know that an entire cell can be made into a hyperlink easily, but not a specific part of the cell as far as I know.
By hyperlink I also refer to either
6 Answers 6
After creating the hyperlink you could format the text in the cell so that only the words of interest are underlined/blue. The hyperlink will still work, but obviously you can still have only one link per cell, and clicking anywhere in the text will trigger the hyperlink.
I needed to link to a filename displayed in a cell, so here is what worked for me:
This isn’t possible in Excel. Hyperlinks are associated with entire cells.
If you look at the documentation for the Excel hyperlink object, you can see that it’s associated with a Range. If it were possible to associate hyperlinks with a span within the cell, the Hyperlink object would need to have an associated Range and Characters object.
The above one liner was very helpful. since I’m new, I couldn’t comment. So here is my variation of the above that takes each row on a worksheet and builds a URL from a value on the row.
I’d just make your one row into two rows, merge the cells in the columns you need to have it appear to be a single row and when you get to the cell that needs the hyperlink then you put the words on the top cell and the link in the cell below. It will look fine as a non-techy workaround.
Here’s a great smoke-and-mirrors solution I’ve used for creating hyperlinked strings within a larger block of text in an Excel spreadsheet cell. CAUTION — if there are multiple editors for your worksheet, this is not advisable as hyperlinks can get misaligned with the text in their cells unless you can provide sufficient protection. A means of doing that is described in this procedure though may limit what contributors can do: