Overview of the Shell Function
The Shell function is used to run an executable program using its file location from inside a VBA code. This function takes two arguments–
Shell(pathname, [windowstyle])
- The first argument, pathname, represents the file location of the executable program on the local storage.
- The second argument, [windowstyle], specifies the window style for the program (optional).
To configure the pathname correctly and open a hyperlink in the Chrome browser, follow these steps:
- Determine the file location of Chrome’s executable program. By default, it is located at C:\Program Files (x86)\Google\Chrome\Application.
- For the second part, you can either hardcode the hyperlink directly in the VBA code or retrieve it from a cell value in an Excel worksheet.
Entering Code in the Visual Basic Editor
To open hyperlinks in Chrome using VBA, follow these steps:
- Go to the Developer tab in the Excel Ribbon.
- Click the Visual Basic option.
- In the Visual Basic for Applications window, select Insert from the dropdown and choose New Module.
- Enter your code inside the Visual Basic editor and press F5 to run it.
Method 1 – Open a Specific Hyperlink in Chrome with Excel VBA
In this example, we’ll open a hardcoded hyperlink (Exceldemy website) in a new Chrome tab using the following VBA code:
- Copy and paste the VBA code into the Visual Basic Editor and press F5 to run it.
Sub OpenHyperlinkInChrome()
Dim chromeFileLocation As String
Dim hyperlink As String
hyperlink = "https://www.exceldemy.com/"
chromeFileLocation = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
Shell (chromeFileLocation & "-url " & hyperlink)
End Sub
Code Explanation
In the provided code snippet, we define two variables: chromeFileLocation and hyperlink, both as strings. Let’s break down the purpose of each variable:
- chromeFileLocation:
- This variable stores the file location of Chrome’s executable program.
- We enclose the file location within double quotation marks.
- To find the file location, you can click on the address bar in Windows Explorer while Chrome is open.
- hyperlink:
- This variable holds the hyperlink (https://www.exceldemy.com/) for the Exceldemy website.
- We assume that this hyperlink is relevant to the task at hand.
We combine these two variables to define the pathname argument for the Shell function in our VBA code.
Method 2 – Opening a Hyperlink in Chrome Using Cell Reference in Excel VBA Code
Suppose we have a URL in cell B4 of the active worksheet. Our goal is to extract the hyperlink from this cell reference and open it in a Chrome window. Here’s the solution:
- We use the Range.Hyperlinks property of Excel to retrieve the hyperlink associated with the defined cell reference.
- We use the Hyperlinks.Item property to obtain the address as a string.
Below is the VBA code for achieving this.
- Copy and paste the VBA code into the Visual Basic Editor and press F5 to run it.
Sub OpenHyperlinkInChrome()
Dim chromeFileLocation As String
Dim hyperlink As String
hyperlink = ActiveSheet.Range("B4").Hyperlinks.Item(1).Address
chromeFileLocation = """C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe"""
Shell (chromeFileLocation & "-url " & hyperlink)
End Sub
Read More: VBA to Add Hyperlink to Cell Value in Excel
Method 3 – Running a VBA Code to Open Hyperlinks in Chrome Based on Selection
Suppose we have a list of URLs in cells B5:B11, and we want to open these URLs in the same Chrome window but in different tabs based on user selection. Here’s how we achieve this:
- We use the Application.Selection property to get the cell references that hold the selected URLs in the current worksheet.
- To loop through the multiple selections, we use the For Each…Next statement in our VBA code.
Below is the VBA code for this method:
- Copy and paste the VBA code into the Visual Basic Editor and press F5 to run it.
Sub OpenHyperlinksInChrome()
Dim chromeFileLocation, hyperlink As String
Dim Rng, SelectRange As Range
chromeFileLocation = """C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe"""
On Error Resume Next
Set SelectRange = Application.Selection
For Each Rng In SelectRange
hyperlink = Rng.Hyperlinks.Item(1).Address
Shell (chromeFileLocation & "-url " & hyperlink)
Next
End Sub
Notes
- When clicking on a hyperlink in an Excel sheet, it typically opens the link in the computer’s default browser.
- If necessary, you can check and reset the default browser by searching for Default web browser in the start menu and selecting the appropriate option.
Then choose to reset the default browser from the available list.
Download Practice Workbook
You can download the practice workbook from here:
Thank you very much! I’ll try. If can I ask something, is possible scrap in a chrome browser with this?
You are welcome, Henry. Yes, it’s possible. Please share your problem with details at [email protected]
Regards.
Hello
I followed your instructions but often get a 404 error. As you can see from the code I am looking for books using ISBN number. Sometimes it works, sometimes not (more often fail than success).
I have tried all sorts of checks on the ISBN number and can manually get there by typing in an ISBN that has previously failed.
I am bamboozled.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lngRow As Long
Dim strChromeLocation As String
Dim strISBN As String
Dim lngISBN As Single
Dim strURL As String
lngRow = Target.Row
strISBN = Cells(lngRow, 8)
strURL = “https://www.isbnsearcher.com/books/” & strISBN
strChromeLocation = “””C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”””
Shell (strChromeLocation & “-url ” & strURL)
End Sub
Hello JEFF WHALE,
Thank you very much for following our articles.
You mentioned that your sample code is not working properly. We attached a VBA code that will help you to solve this problem. You need to choose a cell from the dataset that contains an ISBN number when running the code. One thing adding that change the location of Chrome according to your computer.
And you are getting this 404 error because without any ISBN number, this isbnsearcher.com/books link will show an error by default.
Sub OpenstrHyperlinkInChrome()
Dim strChromeLocation As String
Dim strURL As String
Dim strISBN As String
strISBN = Application.InputBox("Please Select Desired Cell", Type:=8)
strURL = "https://www.isbnsearcher.com/books/" & strISBN
strChromeLocation = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (strChromeLocation & "-url " & strURL)
End Sub