0 votes
ago in VBA by Beginner (105 points)

I used a "Macro to extract the URL" formula in my excel sheet and I am getting a #VALUE error. Is there something that I missed? I came accross your website because I have been researching ways to extract the url's from hyperlinks I got from a website. However, the VBA macros I have been trying to use do not work fully. I only get part of the url and it does not get anything past the pound sign. As mentioned when I tried using your VBA, I am getting a #VALUE error. Any help would be greatly appreciated because I have over 1000 hyperlinks.

ago by Beginner (105 points)

Full url: https://www.acquisition.gov/content/part-3-improper-business-practices-and-personal-conflicts-interest#i1124578
Link from other VBA: https://www.acquisition.gov/content/part-3-improper-business-practices-and-personal-conflicts-interest    

Other VBA code:  

Function Hlink(rng As Range)
'Extracts the url from the hyperlink
    On Error Resume Next
    Hlink = rng.Hyperlinks(1).Address
    If Hlink = 0 Then Hlink = ""
End Function


1 Answer

+1 vote
ago by Super Expert (1.8k points)
selected ago by
Best answer

To grab the URL anchor (anything after the # pound sign) at the end of the hyperlink, you'll need to call the SubAddress property of the hyperlink. Try modifying your macro to look like this:

Function Hlink(rng As Range)
'Extracts the url from the hyperlink'
    On Error Resume Next
    If rng.Hyperlinks(1).SubAddress <> "" Then
        Hlink = rng.Hyperlinks(1).Address & "#" & rng.Hyperlinks(1).SubAddress
        Hlink = rng.Hyperlinks(1).Address
    End If
    If Hlink = 0 Then Hlink = ""
End Function


ago by Beginner (105 points)
THANK YOU SOOO MUCH!!!!!!!!!! This is a life SAVER!!!!

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts as your contributions grow. The more points you earn, the better the prizes.

Getting Started

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

Top Users Mar 2019
  1. YasserKhalil

    306 Points

  2. ParserMonster

    204 Points

  3. Alberto Semat

    186 Points

  4. danmcg

    127 Points

  5. Abdan

    124 Points

Prizes for March 2019
Terms and Conditions
1st place (1) 1-year MyExcelOnline Academy Membership
(2) My First Add-in Dev Pack & Course
(3) AutoMacro: VBA Code Generator (Developer)
(4) VBA Cheat Sheet Bundle
(5) $35 Amazon Gift Card
2nd place (1) My First Add-in Dev Pack & Course
(2) 101 Most Popular Excel Formulas E-Book
(3) VBA Cheat Sheet Bundle
(4) $25 Amazon Gift Card
3rd place (1) Mouse to Macro
(2) VBA Cheat Sheet Bundle
(3) $15 Amazon Gift Card