+ Reply to Thread
Results 1 to 5 of 5

VBA loop through column and extract embedded hyperlink and create clickable hyperlink

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2014
    Location
    Florida
    MS-Off Ver
    Excel 2016
    Posts
    62

    VBA loop through column and extract embedded hyperlink and create clickable hyperlink

    Hello,

    I have data in a spreadsheet column 4 that is formatted like this:

    "DOC1#http://acmecorp.com/urlfiles/Doc1.pdf"

    This an export from MS Access table and the inclusion of the "#" allows the hyperlink to be clickable within the table (MS Accesses way of creating the "friendly name" and "location" in one line.

    Additionally, some of the rows have empty column cells and others have just static text without a hyperlink - i.e. "Document unavailable", or "N/A". These just need to be ignore.

    I'd like to loop through all rows to reformat the data in each cell so that it's now clickable via the Excel output file.

    So the Visible/Friendly name is "DOC1" and the location is the "Http://acmecorp.com/urlfiles/Doc1.pdf" And the "#" is just removed altogether.

    VBA needed:
    • For loop to check each row in sheet and check data in column 4 cell
    • See if there is a hyperlink in the cell "i.e. Find *http://" Then if it's there
    • Copy all text AFTER the "#" character
    • Store in string
    • Delete everything AFTER and INCLUDING the "#" to leave just the name of the doc
    • Use remaining text in cell (i.e. DOC1) as the Visible part of the hyperlink
    • Paste copied hyperlink from string as the "location" part of the hyperlink
    • If "http://" is not there, just ignore and move on to next row

    Can the HYPERLINK function be used? (HYPERLINK(link_location, [friendly_name]))

    Thank you in advance for your assistance.

  2. #2
    Registered User
    Join Date
    02-17-2014
    Location
    Florida
    MS-Off Ver
    Excel 2016
    Posts
    62

    Re: VBA loop through column and extract embedded hyperlink and create clickable hyperlink

    Update - I've tried the following, but get an error at the ".Hyperlink.Add" part. (Run-time error '483': Object doesn't support this property or method)

    Private Sub TestMe()
    Dim lastrow As Integer
    Dim contents As String
    Dim link As String
    Dim friendly As String
    Dim i As Integer
    
    With ActiveSheet
    
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To lastrow
    
    contents = .Range("L" & i).Value
    
    If Len(contents) > 20 Then
    
    friendly = Left(contents, InStr(contents, "#") - 1)
    
    link = Right(contents, Len(contents) - InStr(contents, "#"))
    
    .Hyperlink.Add Anchor:=.Range("L" & i), Address:=link, TextToDisplay:=friendly
    
    Else
    End If
    
    Next i
    
    End With
    
    End Sub

  3. #3
    Registered User
    Join Date
    03-10-2021
    Location
    Paryzevo
    MS-Off Ver
    2k3
    Posts
    60

    Re: VBA loop through column and extract embedded hyperlink and create clickable hyperlink


    Maybe this way ?
    Option Explicit
    
    Sub Hyper_Add_Links()
        ' DOC1#http://acmecorp.com/urlfiles/Doc1.pdf
        Const crt1 = "#http://"
        Const crt2 = ".pdf"
        Const dlmtr = "#"
        
        Dim c As Long, r As Long, rws As Long
        Dim sadr As String, st As String
        Dim tbl, txt
        
        c = 4
        sadr = ""
        st = "Click me, Click me, Click me .... :)"
        
        With ThisWorkbook.Sheets("Sheet1")
            rws = .Cells(.Rows.Count, c).End(xlUp).Row
            tbl = .Range(Cells(1, c), Cells(rws, c)).Value
            For r = 1 To rws
                If InStr(1, tbl(r, 1), crt1, 0) <> 0 Then
                    If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then
                        txt = Split(Application.Clean(tbl(r, 1)), dlmtr, -1, 0)
                        ' .Cells(r, c) => only if column #4 has no header
                        .Hyperlinks.Add Anchor:=.Cells(r, c), _
                                        Address:=Trim$(txt(1)), SubAddress:=sadr, _
                                        ScreenTip:=st, TextToDisplay:=Trim$(txt(0))
                    End If
                End If
            Next
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    02-17-2014
    Location
    Florida
    MS-Off Ver
    Excel 2016
    Posts
    62

    Re: VBA loop through column and extract embedded hyperlink and create clickable hyperlink

    Hi mjr veverka,

    I'm running this code from within MS Access since my Access code exports data to an Excel sheet and then manipulates the Excel sheet (i.e. formatting, coloring, etc.). But this all happens from within the MS Access database VBA.

    I modified your code slightly to update the column number as well as to account for Access not recognizing the "Application.Clean" command. But I get an error on the line with "If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then" it highlights "tbl" with error "Compile Error ByVal Argument Type Mismatch".

    Here's the code:

    Error Happens at this line:
                    If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then

    This allows the use of the "Clean" feature.
    Function MyClean(x As String) As String
        MyClean = Excel.Application.WorksheetFunction.Clean(x)
    End Function
    Modified code you provided:
    Public Sub HyperAdd()
        Const crt1 = "#http://"
        Const crt2 = ".pdf"
        Const dlmtr = "#"
        
        Dim c As Long, r As Long, rws As Long
        Dim sadr As String, st As String
        Dim tbl, txt
        
        c = 12
        sadr = ""
        st = "Click to Open"
        
        With ActiveSheet
            rws = .Cells(.Rows.Count, c).End(xlUp).Row
            tbl = .Range(Cells(1, c), Cells(rws, c)).value
            For r = 1 To rws
                If InStr(1, tbl(r, 1), crt1, 0) <> 0 Then
                    If Right$(RTrim$(tbl(r, 1)), Len(crt2)) = crt2 Then
                        txt = Split(MyClean(tbl(r, 1)), dlmtr, -1, 0)
                        .Hyperlinks.Add Anchor:=.Cells(r, c), _
                                        Address:=Trim$(txt(1)), SubAddress:=sadr, _
                                        ScreenTip:=st, TextToDisplay:=Trim$(txt(0))
                    End If
                End If
            Next
        End With
    The code runs flawlessly in Excel VBA, just not from the MS Access VBA side. I guess I could pass the code to Excel and execute this from within Excel, but I'll have to research that.

    Any thoughts?
    Last edited by Jay S.; 04-26-2021 at 12:46 PM.

  5. #5
    Registered User
    Join Date
    02-17-2014
    Location
    Florida
    MS-Off Ver
    Excel 2016
    Posts
    62

    Re: VBA loop through column and extract embedded hyperlink and create clickable hyperlink

    I was able to get the following code to work. In my case, I'm exporting data from MS Access to Excel and manipulating the data in Excel FROM MS Access VBA, so it posed a few issues with code recognition. This, however, worked.

    Dim lastrow As Integer
    Dim contents As String
    Dim link As String
    Dim friendly As String
    Dim i As Integer
    
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    
    For i = 1 To lastrow
    
    contents = .Range("L" & i).value
    If Len(contents) > 20 Then
    friendly = Left(contents, InStr(contents, "#") - 1)
    link = Right(contents, Len(contents) - InStr(contents, "#"))
    .Range("L" & i).Formula = "=Hyperlink(""" & link & """, """ & friendly & """)"
    Else
    End If
    Next i
    Last edited by Jay S.; 04-26-2021 at 04:44 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. userfom load value txtbox hyperlink to cell and create short hyperlink
    By remco77a in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2020, 08:38 AM
  2. Hyperlink to open embedded object for a column
    By carlrubber in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-13-2016, 11:38 PM
  3. Create Clickable Hyperlink in Word Document from excel Userform
    By craig62 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2014, 08:27 AM
  4. [SOLVED] Excel 2010 Create a macro to check if cell contains hyperlink then apply hyperlink style
    By chasidar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2013, 04:48 AM
  5. Excel 2007 : Clickable hyperlink that leads to a web page
    By imran9171 in forum Excel General
    Replies: 2
    Last Post: 02-29-2012, 09:14 PM
  6. Replies: 3
    Last Post: 01-13-2012, 12:11 PM
  7. Extract name and create a hyperlink
    By Ashish82 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-23-2008, 12:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1