Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Opening Sites in a Browser.
Written by Allen Wyatt (last updated December 3, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Steve has a range of cells (A1:A10) that contains website addresses—for example, www.example.com. He wonders if it is possible, within a macro, to open each of these addresses in a browser all at once in separate browser tabs.
There are a couple of ways you can approach this task, and which one you choose depends largely on the nature of the data in your worksheet. If the cells contain active hyperlinks (ones that if you click on them, the address is opened in a browser), then you can use a rather simple macro:
Sub FollowHyperlinks1() Dim MyRange As Range Dim hl As Hyperlink On Error Resume Next Set MyRange = Range("A1:A10") For Each hl In MyRange.Hyperlinks hl.Follow Next hl End Sub
The macro simply looks at all the hyperlinks in the range of A1:A10 and uses the Follow method to open each of them in your default browser. Because of the way in which your operating system transfers information from Excel to your browser, it is a good idea to have your browser open before you run the macro. The reason for this is because, in testing, we found that you may actually end up with two instances of the browser open, with some addresses open in one instance and some in the other. This apparently occurs because of the delay in opening the first instance of the browser. If the browser is open before the macro is run, then there is no delay and each address opens in a different tab of the same browser instance.
If the addresses in your worksheet may not be active hyperlinks, then you can't rely upon using the Hyperlinks collection for the range. Instead, you need to look at the value of each cell in the range:
Sub FollowHyperlinks2() Dim MyRange As Range Dim cell As Range Dim sTemp As String On Error Resume Next Set MyRange = Range("A1:A10") For Each cell In MyRange sTemp = cell.Value ThisWorkbook.FollowHyperlink Address:=sTemp Next cell End Sub
This approach uses the FollowHyperlink method to load the address in the sTemp variable. In this case, it doesn't matter whether the contents of the cells are active hyperlinks or not; the code still tries to open them in a browser.
Finally, if your data may not contain fully qualified addresses, then you'll need to use a different approach, still. For instance, Steve mentioned having addresses such as www.example.com in the worksheet, but such an address will not work with the examples so far. If your data is missing http:// at the beginning (or some variant, such as https://), then the code won't open the address in the browser. If your data has this peculiarity, then a slight modification to the macro is in order:
Sub FollowHyperlinks3() Dim MyRange As Range Dim cell As Range Dim sTemp As String On Error Resume Next Set MyRange = Range("A1:A10") For Each cell In MyRange sTemp = cell.Value If InStr(sTemp, "://") = 0 Then sTemp = "http://" & sTemp End If ThisWorkbook.FollowHyperlink Address:=sTemp Next cell End Sub
Note that this example examines the contents of sTemp to see if it has the characters "://" within it. If not, then the prefix http:// is added to the cell contents and Excel tries to use the FollowHyperlink method to open the modified address.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11414) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Opening Sites in a Browser.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
When you send a macro-enabled workbook to someone, that person may have problems opening and using it. This tip looks at ...
Discover MoreIf you use Excel's Web Query tools to grab data from a website, you may run into some problems if the site isn't ...
Discover MoreGot a single worksheet that you want to e-mail to someone, but don't want them to see the rest of the worksheets in the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2024 Sharon Parq Associates, Inc.
Comments