+ Reply to Thread
Results 1 to 7 of 7

Using vba to hyperlink cells when populated

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Post Using vba to hyperlink cells when populated

    Hi All,

    Is there a way of using VBA to create a hyperlink to a cell only when the cell is populated, for eg. All cells in colum E will have a reference sheet number (12345678) and when selected (clicked on) will look in C:\Documents and Settings\carl.walker\My Documents\Machine DWG for that file named 12345678 and open it?

    The cell currently gets populated by a userform (if that makes any odds).
    I have uploaded a copy of the workbook to veiw.


    Any help or guidance would be much appreciated.

    Thanks in advance
    Carl
    Attached Files Attached Files

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Using vba to hyperlink cells when populated

    Try adding the following to the Sheet1 tab

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    Application.EnableEvents = False
    For Each Cell In Target
        If Cell.Column = 5 Then
            If Cell.Value <> "" Then
                ActiveSheet.Hyperlinks.Add Anchor:=Cell, Address:= _
                "C:\Documents and Settings\carl.walker\My Documents\Machine DWG\" & Cell.Value & ".txt" ', TextToDisplay:=Cell.Value
            Else
                Cell.Hyperlinks.Delete
            End If
        End If
    Next Cell
    Application.EnableEvents = True
    End Sub
    I've assumed that the file has a .txt extension.
    Martin

  3. #3
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Using vba to hyperlink cells when populated

    Hi Martin,

    Thanks for your work.

    When clicking on the hyperlink the msg that appears is "Cannot open the specified file". You mentioned does the file have a txt extention? Im not clear what this is but the file that I am trying to open is pdf if that helps.
    The text appears as if it is a hyperlink and when you hover over it, it displays the correct directory too.

    Kind Regards
    Carl

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Using vba to hyperlink cells when populated

    OK - can you try

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    Application.EnableEvents = False
    For Each Cell In Target
        If Cell.Column = 5 Then
            If Cell.Value <> "" Then
                If Dir("C:\Documents and Settings\carl.walker\My Documents\Machine DWG\" & Cell.Value & ".pdf") <> "" Then
                    ActiveSheet.Hyperlinks.Add Anchor:=Cell, Address:= _
                    "C:\Documents and Settings\carl.walker\My Documents\Machine DWG\" & Cell.Value & ".pdf", TextToDisplay:=Cell.Value
                End If
            Else
                Cell.Hyperlinks.Delete
            End If
        End If
    Next Cell
    Application.EnableEvents = True
    End Sub

  5. #5
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Using vba to hyperlink cells when populated

    Hi Martin,


    I have put the code in sheet1 but nothing happens, none of the cells appear to have hyperlinks unlike the first code.
    I have uploaded the workbook just in case it's me!


    Thanks for your help
    Carl
    Attached Files Attached Files

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Using vba to hyperlink cells when populated

    It's probably that the string that I am using to generate the path and filename is not exactly the same as on your system.

    "C:\Documents and Settings\carl.walker\My Documents\Machine DWG\" & Cell.Value & ".pdf"
    If your replace Cell.Value with the number - does this give the correct path?

  7. #7
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Using vba to hyperlink cells when populated

    Hi Martin,

    I removed all hyperlinks from column E then in the first code you done for me I changed "txt" to "pdf" , then I cut all content from that column and pasted it back in the same column.

    After this that lovely blue line appeared under the Ref number indicating a hyperlink, better still when I clicked on it...it worked

    Thanks for taking the time to help Martin, it's much appreciated!

    Regards
    Carl

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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