How to extract a URL from a hyperlink on Excel

Today, I faced the problem of extracting URLs from a long list of hyperlinked text. I had two options:

First is to do it manually:

  1. Right-click a hyperlink.
  2. From the Context menu, choose Edit Hyperlink. Excel displays the Edit Hyperlink dialog box.
  3. Select and copy (Ctrl+C) the entire URL from the Address field of the dialog box.
  4. Press Esc to close the Edit Hyperlink dialog box.
  5. Paste the URL into any cell desired.

But then I have a long list of hyperlinks in my Excel worksheet. If I had to do this for each and every single hyperlink, this can get tedious very very quickly. So the second option is to get the URLs using a macro.

The following example can be useful when extracting hyperlinks from tables of links that have been copied into Excel, when doing post-processing on Excel web queries, or getting the email address from a list of “mailto:” hyperlinks.

Extracting a URL from a hyperlink on Excel is easy!

  1. Open up a new workbook.
  2. Get into VBA (Press Alt+F11)
  3. Insert a new module (Insert > Module)
  4. Copy and Paste the Excel user defined function below
  5. Get out of VBA (Press Alt+Q)
  6. Use this syntax for this custom Excel function: =GetURL(cell,[default_value])

Function GetURL(cell As range, _
Optional default_value As Variant)
'Lists the Hyperlink Address for a Given Cell
'If cell does not contain a hyperlink, return default_value
If (cell.range("A1").Hyperlinks.Count <> 1) Then
GetURL = default_value
Else
GetURL = cell.range("A1").Hyperlinks(1).Address
End If
End Function

This entry was posted in Macros. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

48 Comments

  1. covaz
    Posted January 25, 2012 at 2:21 pm | Permalink

    Thank you!!! You just saved me about 30 mins of work!

  2. karen
    Posted January 25, 2012 at 3:26 pm | Permalink

    You are welcome! I am happy that my post saved you a lot of time!

  3. dhiraj das
    Posted May 14, 2012 at 3:01 am | Permalink

    Thanks a lot!! Could save my time.

  4. Bruno
    Posted June 17, 2012 at 8:52 am | Permalink

    Good stuff. Big thanks to Karen.

  5. Jesse
    Posted July 2, 2012 at 1:31 pm | Permalink

    Ummmm, these are horrible instructions for a total newb like me. How do I apply this to all of my cells with anchor-texted-hyperlinks in them?

  6. Sarah
    Posted September 4, 2012 at 3:22 am | Permalink

    You are a life saver! that’s 4000+ lines worth of work.. :) thanks!

  7. Posted September 13, 2012 at 7:03 am | Permalink

    If you were in the same room as me right now I would hug you. Life saver!

  8. karen
    Posted September 13, 2012 at 7:42 am | Permalink

    So happy I could help :)

  9. Jeff
    Posted October 4, 2012 at 10:13 am | Permalink

    Hi Karen,
    Seems like this could save me a ton of time, however I keep getting an error message.
    Operating in Excel 2007
    I followed your directions
    When I hit enter I go immediately back to MVB
    Pop Up Compile error: Invalid outside procedure
    Book 2 – Sheet 2 (Code)
    Set rng = rng(1)

    If rng.Hyperlinks.Count = 0 Then
    GetURL = “B”
    Else
    GetURL = rng.Hyperlinks(1).Address
    End If
    End Function

    Behind that in another box is
    Book 2 – Module1 (Code)
    Function GetURL(cell As Range, _
    Optional default_value As Variant)
    ‘Lists the Hyperlink Address for a Given Cell
    ‘If cell does not contain a hyperlink, return default_value
    If (cell.Range(“A1″).Hyperlinks.Count 1) Then
    GetURL = default_value
    Else
    GetURL = cell.Range(“A1″).Hyperlinks(1).Address
    End If
    End Function

    I do not have super strong excel skills but follow directions well. Please help me if you can. Thank you in advance, I am certain I am doing something small wrong
    -Jeff

  10. karen
    Posted October 4, 2012 at 10:44 am | Permalink

    Jeff, I’m sorry to hear that you are getting these errors. You mentioned that you are seeing the error when you hit “enter”. Can you clarify on which step you are hitting “enter”? I don’t think there’s a step that requires you to do that. I Did you try these steps below? Can you try these again?

    1. Open up a new workbook.
    2. Get into VBA (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste the Excel user defined function above.
    5. Get out of VBA (Press Alt+Q)
    6. Use this syntax for this custom Excel function: =GetURL(cell,[default_value])

  11. Lacoste
    Posted October 6, 2012 at 2:46 am | Permalink

    Morning, I thought the whole day going to the 4700 lines, but now half a minute to complete. Thank you very much!

  12. karen
    Posted October 6, 2012 at 10:35 am | Permalink

    Glad to hear this saved you time! :)

  13. Steve Loos
    Posted October 18, 2012 at 1:56 pm | Permalink

    Thank you for posting this! However I followed your steps. I am familiar with excel functions.
    Your code assumes the email “mailto” url is in cell A1, correct? This is where I have hyperling “john smith” which is “mailto@johnsmit@mail.com”
    I put the =GetURL(cell,[default_value]) into cel B1 but nothing happens, or I get an error.
    I am sure I am close!
    Steve; excel 2010.

  14. karen
    Posted October 18, 2012 at 2:06 pm | Permalink

    Hi Steve! You are indeed very close! Just put =GetURL(A1) and that should work. If you have an entire column of URLs, you can just drag that formula all the way down. Let me know in case it doesn’t work. Good luck!

  15. Posted December 13, 2012 at 8:02 pm | Permalink

    I’m trying to extract email addresses from hyperlinked picture objects (email symbols) which I have placed in an Excel file. I followed your steps. But it is not doing the job. Any suggestions? Best regards, Donnell

  16. karen
    Posted December 13, 2012 at 8:58 pm | Permalink

    I am sorry to hear that they didn’t work? Did you try these steps below? It should be pretty simple.

    1. Open up a new workbook.
    2. Get into VBA (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste the Excel user defined function above.
    5. Get out of VBA (Press Alt+Q)
    6. Use this syntax for this custom Excel function: =GetURL(cell,[default_value])

  17. karen
    Posted December 13, 2012 at 9:02 pm | Permalink

    Hi Donnell! I have not tried this method for hyperlinked pictures, but I suspect that it won’t work because pictures are not really within cells. The only thing I can think of is doing it manually, but that’s really tedious. If there’s no way to do it except manually, consider hiring someone on Odesk.com to do it for you for a few bucks instead :) I am sorry that I couldn’t be of more help!

  18. Chevy
    Posted December 20, 2012 at 3:47 pm | Permalink

    Thank you VERY much!

  19. karen
    Posted January 10, 2013 at 6:27 pm | Permalink

    You’re welcome, Chevy! Glad I was able to help :)

  20. Posted January 21, 2013 at 11:22 am | Permalink

    it’s worked, it worked!!! thanks!!!!! :)

  21. karen
    Posted January 21, 2013 at 7:02 pm | Permalink

    Happy to hear that it worked for you!

  22. Jake
    Posted January 27, 2013 at 10:54 am | Permalink

    Thanks!!!

  23. karen
    Posted January 27, 2013 at 11:13 am | Permalink

    You’re welcome, Jake! :)

  24. Lowrance
    Posted February 1, 2013 at 2:28 am | Permalink

    Wow it worked :D even in 2013 version file…Thank you very much :)

  25. karen
    Posted February 1, 2013 at 8:35 am | Permalink

    So happy it worked for you, Lowrance! :)

  26. Brian
    Posted February 13, 2013 at 11:37 am | Permalink

    Okay, I understand the macro part but what is step 6 referring to with “Use this syntax for this custom Excel function: =GetURL(cell,[default_value])”

    What is a syntax, where do I copy and paste “=GetURL(cell,[default_value])”

    If helps, the hyperlinks I’m trying to convert are in cells ranging from G18 to G50.

  27. effectsalive
    Posted February 14, 2013 at 6:52 pm | Permalink

    Making use of =hyperlink formula with text and multiple cells. At first this did not work, but copy and paste into word then back to excel and now I got all the full URLs!

  28. karen
    Posted February 15, 2013 at 9:31 am | Permalink

    Thank you for your suggestion! I have not tried that function as a way to extract URLs, but I’m happy to hear that it worked for you :)

  29. karen
    Posted February 15, 2013 at 9:36 am | Permalink

    Brian, you can just use the function “=GetURL(cell,[default_value])” on your Excel sheet as you would any other standard Excel formula. For example, if cell A1 contains the hyperlinked text whose URL who you want to extract, you can just type “=GetURL(A1)” on any other cell on the sheet to extract the hyperlink. Let me know if I can answer any other questions! I’ll be happy to help.

  30. Jason
    Posted March 18, 2013 at 3:12 am | Permalink

    How can I save it permanently in excel? Because everytime I open the excel this function is gone

  31. Bixplat
    Posted March 19, 2013 at 9:04 am | Permalink

    I think where some people are getting confused is with the [default_value] portion. Here is an example of what will work. Data is in Row 1 Column A. Click on Row 1, Column B. Use =GetURL(A1,) . This should work.

  32. Paul Miller
    Posted March 20, 2013 at 4:47 pm | Permalink

    This ROCKS!! Saved me some tedious work! THANK YOU!!

  33. karen
    Posted March 20, 2013 at 8:07 pm | Permalink

    Paul, it makes me so happy to hear that I’ve saved you time :) Glad to hear this post helped!

  34. taylor
    Posted April 3, 2013 at 10:34 am | Permalink

    I’m getting everything to work Karen but for some reason the cell says mailto:(person’semailaddress) but when I click it to try and copy and past it just says the formula again. What am i doing wrong? I just want it to read as the person’s email address it would save me hours of time.

  35. karen
    Posted April 3, 2013 at 3:50 pm | Permalink

    Oh that’s easy to do, Taylor. Just follow these steps:

    1. Highlight the entire column containing the formulas.

    2. Press CTRL-C on your keyboard to copy the content of all the cells in that column.

    3. Right-click and select “Paste Special…”

    4. Select “Values”.

    6. Click the “OK” button.

    7. Press CTRL-F to launch the “Find” popup.

    8. Enter “mailto:” next to “Find what:”.

    9. Click the “Replace” tab.

    10. Click the “Replace All” button.

    Hope this helps!

  36. khan
    Posted April 4, 2013 at 12:04 am | Permalink

    awesome.. :)

  37. karen
    Posted April 4, 2013 at 12:06 am | Permalink

    Glad you found it helpful, Khan :)

  38. RohitSharmaCREATRIX
    Posted April 16, 2013 at 3:59 am | Permalink

    Great Code… Worked Well, and saved huge time….

    Cheers..

  39. Posted April 20, 2013 at 5:02 am | Permalink

    I was trying in office 2010 but not working to get the link.. please help

  40. karen
    Posted April 22, 2013 at 12:56 pm | Permalink

    Jagan, sorry to hear that it was not working for you. I believe it does work for Office 2010 too. Could it be that your macros are just disabled? At which step is it failing?

  41. karen
    Posted April 22, 2013 at 5:47 pm | Permalink

    Glad it helped, Rohit!

  42. karen
    Posted April 22, 2013 at 5:50 pm | Permalink

    Jason, if you are using the same Excel file, it should remain there. However, if you are creating a new file, you have to re-insert the macro.

  43. oscar
    Posted April 25, 2013 at 6:38 pm | Permalink

    Fantastic! now imagine if we could sum up all the hours that you saved from everyone I’d guess a few thousands hrs.

    Thanks a bunch
    OG

  44. karen
    Posted April 25, 2013 at 7:33 pm | Permalink

    Oscar, I’m so happy to hear that it saved you a lot of time :)

  45. VG
    Posted April 29, 2013 at 9:46 am | Permalink

    Thank you, your post saved almost half a day for me

  46. karen
    Posted May 14, 2013 at 2:40 pm | Permalink

    So happy that my post helped you save so much time :)

  47. Posted May 16, 2013 at 4:13 pm | Permalink

    Thanks for this :)

  48. karen
    Posted May 16, 2013 at 4:52 pm | Permalink

    You’re very welcome, Brian :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>