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: Converting a Range of URLs to Hyperlinks.
Written by Allen Wyatt (last updated April 27, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
John has a workbook that has well over a thousand URLs in it, all in column A. These are not hyperlinks; they are straight text of individual URLs. John wants to convert the URLs to active hyperlinks, but doing the conversion individually is extremely tedious, especially for that many URLs.
One way to do the conversion is to use the HYPERLINK function. Put this formula in cell B1 and copy it down as many cells as necessary:
=IF(A1="","",HYPERLINK(IF(LEFT(A1,7)="http://","","http://")&A1))
The result is that column B will contain hyperlinks for everything in column A. The formula isn't terribly robust, as it only deals with the presence or lack of the text "http://", and you may need it to also deal with "https" addresses. It is possible to adjust the formula (i.e., making it more complex), but you may also want to consider using a macro to do the conversions.
To be effective, the macro would need to step through each cell in a selected range and, if the cell is not blank, convert the contents to a hyperlink. The following will do the trick:
Sub URL_List() Dim sTemp As String Dim sURL As String Dim cell As Range For Each cell In Selection If cell.Value <> "" Then sTemp = Left(cell.Value, 7) If sTemp = "http://" Or sTemp = "https:/" Then sURL = cell.Value Else sURL = "http://" & cell.Value End If ActiveSheet.Hyperlinks.Add Anchor:=cell, _ Address:=sURL, TextToDisplay:=cell.Value End If Next cell End Sub
The macro is not foolproof; it assumes that if a cell contains anything at all it is a valid URL. What it does is to check the cell contents and, if the contents aren't prefaced by the "http://" or "https:/" text, then a prefix of "http://" is added. The hyperlink is then created based on the cell contents.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5825) 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: Converting a Range of URLs to Hyperlinks.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Hyperlinks can be a great timesaver and very convenient. Unless, of course, if they don't work as you expect. This tip ...
Discover MoreIf you have a list of hyperlinked e-mail addresses in a worksheet, you may want to extract the addresses from those ...
Discover MoreExcel should allow you to both add and remove hyperlinks in a worksheet. If you run into problems removing hyperlinks, ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-10-09 10:24:33
J. Woolley
@AJ
Re. my most recent comment below: On third thought, simply replace the following line
ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=sURL
with these lines
On Error Resume Next
ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=sURL
On Error GoTo 0
This should work in either the Tip's version or in my first version of the macro. It skips over cell values that do not make valid http hyperlinks.
2024-10-08 16:58:55
J. Woolley
@AJ
Re. my previous comment below: On second thought, replace the following line
ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=sURL
with these lines
Dim n As Integer
n = InStr(Mid(sURL, InStr(sURL, "://") + 4), "/")
If n = 0 Then n = Len(sURL)
If InStr(Left(sURL, n), " ") + InStr(Left(sURL, n), "%") = 0 Then
ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=sURL
End If
because the address part of a URL must not contain any space characters and the Hyperlinks.Add method will not accept %20 as a substitute. Spaces in the subaddress part are automatically replaced by %20. For example, consider the following cell values:
http://my site.com -- cannot create hyperlink
http://my%20site.com -- cannot create hyperlink
http://mysite.com/a b c/ -- hyperlink is http://mysite.com/a%20b%20c/
2024-10-08 11:05:44
J. Woolley
@AJ
You probably had leading space characters in some cells. Try this version:
Sub URL_List()
Dim sTmp As String, sURL As String, cell As Range
'WARNING: This macro assumes web site (http) URLs
For Each cell In Selection
sURL = Trim(cell.Value)
If sURL <> "" Then
sTmp = Left(sURL, 7)
If sTmp <> "http://" And sTmp <> "https:/" Then
sURL = "http://" & sURL
End If
ActiveSheet.Hyperlinks.Add Anchor:=cell, Address:=sURL
End If
Next cell
End Sub
2024-10-07 10:56:35
AJ
Hi
For me, running this code on a selected range, causes a debug (run-time error '1004': Application-defined or object-defined error) at this stage:
ActiveSheet.Hyperlinks.Add Anchor:=cell, _
Address:=sURL, TextToDisplay:=cell.Value
Regards,
AJ
2024-04-28 14:02:53
J. Woolley
My Excel Toolbox includes the following function which is a superior substitute for Excel's HYPERLINK function:
=SuperLink(Link_Location, [Friendly_Name], [Screen_Tip])
SuperLink is fully described in the PDF document UseSuperLink.pdf.
See https://sites.google.com/view/MyExcelToolbox/
2024-04-28 12:31:02
J. Woolley
@Bill Multack
Assuming you are referring to Ctrl+K hyperlinks, not HYPERLINK(...) formulas, select the column of hyperlinks and run this macro:
Sub ShowHyperlinkAddress()
Dim HL As Hyperlink
For Each HL In Selection.Hyperlinks
HL.TextToDisplay = HL.Address
Next HL
End Sub
2024-04-27 09:00:09
Bill Multack
I have just the opposite problem. I have a column of hyperlinks that all have display values. I would like to strip away the TextToDisplay values and only show the hyperlink. Any suggestions?
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