VBA Create Hyperlink in cell to saved file.

Danr

New Member
Joined
May 4, 2011
Messages
30
Hi everyone, I have code that creates a hyperlink in an excel sheet in A1 but I need it to actually hyperlink to the file that was created in the folder. This code I have so far will only open to the folder path not the file path.

Thanks


Code:
 Sub FillMRDT()
    
    'ADD COLUMN NUMBERS AS NECESSARY
    
    'GOOD HABIT TO USE LONG FOR ROW NUMBER VARIABLES
    Dim lRowToCopy As Long, lColCnt As Long
    
    'VARIABLE TO HOLD ARRAY OF COLUMN VALUES
    Dim vColumns As Variant
    Dim wbSSM  As Workbook
    
    Dim strPath As String, strFile As String
    Dim rngHLink As Range
    
    'THIS DETERMINES WHICH ROW THE DATA WILL BE TRANSFERRED TO THE NON-CONFORMANCE FORM
    'Type:=1 validates numbers
    RowToCopy = Application.InputBox("ENTER THE ROW NUMBER TO TRANSFER INFORMATION TO MRDT FORM", "Transfer Row", Type:=1)
    If RowToCopy < 1 Then Exit Sub    'User canceled
    
    'ASSIGN THE VALUES IN EACH COLUMN TO THE VARIABLES, IF MORE
    'COLUMNS, JUST ADD MORE BY CHANGING 'T' TO CORRECT COLUMN
    
    'COPY THE VALUES OF THIS ROW INTO AN ARRAY
    vColumns = Range("A" & RowToCopy & ":v" & RowToCopy).Value
    'MRDT No VColumns (1,1)
    'DATE in vColumns(1,2)
    'CUSTOMER ORDER# in vColumns(1,3)
    'DEPARTMENT in vColumns(1,4)
    'CLOCK# in vColumns(1,5)
    'PART# in vColumns(1,6)
    'QTY in vColumns(1,7)
    'COLOUR IN vColumns (1,8)
    'DEFECT CODE in vColumns(1,9)
    'REASON FOR REJECTION in vColumns (1,10)
    'PRODUCTION WEEK#(1,12)
    'COMMENTS in vColumns(1,13)
    
    
    'CHECK FOR VALID INPUT
    If vColumns(1, 2) = "" Then
        MsgBox ("DATE, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 3) = "" Then
        MsgBox ("CUSTOMER ORDER#, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 4) = "" Then
        MsgBox ("DEPARTMENT, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 5) = "" Then
        MsgBox ("CLOCK#, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 6) = "" Then
        MsgBox ("PART#, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 7) = "" Then
        MsgBox ("QUANTITY, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 8) = "" Then
        MsgBox ("COLOUR, IS NEEDED TO CONTINUE OR USE N/A")
        GoTo CleanUp
    ElseIf vColumns(1, 9) = "" Then
        MsgBox ("DEFECT CODE, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 10) = "" Then
        MsgBox ("REASON FOR REJECTION, IS NEEDED TO CONTINUE")
        GoTo CleanUp
    ElseIf vColumns(1, 12) = "" Then
        MsgBox ("PRODUCTION WEEK#, IS NEEDED TO CONTINUE")
        GoTo CleanUp
        
    End If
    
    Set rngHLink = Range("A" & RowToCopy) 'Hyperlink cell
    
    'NOW TURN THEM ALL TO UPPER CASE. WORKING IN ARRAYS IS VERY FAST
    For lColCnt = 1 To UBound(vColumns, 2)
        vColumns(1, lColCnt) = UCase(vColumns(1, lColCnt))
    Next lColCnt
    
    'CHANGE FILE PATH BELOW TO NON CONFORMANCE LOG
    'ALWAYS SET A WORKBOOK VARIABLE TO A FILE YOU OPEN SO YOU CAN ADDRESS THAT FILE PROPERLY AND KNOW WHAT YOU ARE DOING IN WHICH FILE.
    Set wbSSM = Workbooks.Open(Filename:="P:\Non-Conformance Log\MRDT Form\MRDT Tag.xlsm")
    
    'SAVE THE ROW VALUES TO PARTICULAR FIELDS IN THE MASTER
    With wbSSM.Sheets("MRDT")
    
        .Range("M1").Value = vColumns(1, 1)    'MRDT Number
        .Range("A5").Value = vColumns(1, 3)    'CUSTOMER ORDER#
        .Range("D5").Value = vColumns(1, 12)   'PRODUCTION WEEK#
        .Range("G5").Value = vColumns(1, 4)    'DEPARTMENT
        .Range("J5").Value = vColumns(1, 5)    'CLOCK#
        .Range("L5").Value = vColumns(1, 2)    'DATE
        .Range("A9").Value = vColumns(1, 6)    'PART NUMBER
        .Range("D9").Value = vColumns(1, 7)    'QTY
        .Range("G9").Value = vColumns(1, 8)    'COLOUR
        .Range("A13").Value = vColumns(1, 10)  'REASON FOR REJECTION
        .Range("A22").Value = vColumns(1, 20)  'COMMENTS
    
        MsgBox "ADD ANY ADDITIONAL INFORMATION AND PICTURES INTO SQD FORM AND SAVE"
    
        'THIS WORKBOOK IS THE WORKBOOK HOLDING THIS MACRO
    
        strPath = "P:\Non-Conformance Log\2017"
        strFile = .Range("M1") & " " & .Range("A9").Value
        
    
    End With
        
    'THIS SAVES SQD FILE AS FILENAME IN CREATED FOLDER
    ActiveWorkbook.SaveAs Filename:=strPath & strFile
    
    'Create Hyperlink
    rngHLink.Hyperlinks.Add Anchor:=rngHLink, Address:=strPath
    
    
CleanUp:
    Set wbSSM = Nothing
    
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
doe strFile begin with a \ ?

also you only set folder path in your hyperlink...
Code:
rngHLink.Hyperlinks.Add Anchor:=rngHLink, Address:=strPath

so windows explorer only opens the directory (default behavior when asking to open a directory path)
 
Last edited:
Upvote 0
Hi,
try:

See Cerfanis post. Not sure if Windows handles the missing backslash when you do the saveas. You may need to omit it from bellow if it is coming from strFile.

Code:
FilePth= strPath & "\" & strFile

Address:=FilePth
 
Last edited:
Upvote 0
Hi,
try:

See Cerfanis post. Not sure if Windows handles the missing backslash when you do the saveas. You may need to omit it from bellow if it is coming from strFile.

Code:
FilePth= strPath & "\" & strFile

Address:=FilePth
Can you please place in my Code as an example?
 
Upvote 0
Code:
  'THIS WORKBOOK IS THE WORKBOOK HOLDING THIS MACRO
    
        strPath = "P:\Non-Conformance Log\2017"
        strFile = .Range("M1") & " " & .Range("A9").Value
        [COLOR="#FF0000"]FilePth= strPath & "\" & strFile[/COLOR]
    
    End With
        
    'THIS SAVES SQD FILE AS FILENAME IN CREATED FOLDER
    ActiveWorkbook.SaveAs Filename:=strPath & strFile
    
    'Create Hyperlink
    rngHLink.Hyperlinks.Add Anchor:=rngHLink, Address:=[COLOR="#FF0000"]FilePth[/COLOR]
    
    
CleanUp:
    Set wbSSM = Nothing
    
End Sub
 
Upvote 0
Code:
  'THIS WORKBOOK IS THE WORKBOOK HOLDING THIS MACRO
    
        strPath = "P:\Non-Conformance Log\2017"
        strFile = .Range("M1") & " " & .Range("A9").Value
        [COLOR=#FF0000]FilePth= strPath & "\" & strFile[/COLOR]
    
    End With
        
    'THIS SAVES SQD FILE AS FILENAME IN CREATED FOLDER
    ActiveWorkbook.SaveAs Filename:=strPath & strFile
    
    'Create Hyperlink
    rngHLink.Hyperlinks.Add Anchor:=rngHLink, Address:=[COLOR=#FF0000]FilePth[/COLOR]
    
    
CleanUp:
    Set wbSSM = Nothing
    
End Sub

This change did not work. When I click on the Hyperlink it says it cannot open the source file
 
Upvote 0
Code:
  'THIS WORKBOOK IS THE WORKBOOK HOLDING THIS MACRO
    
        strPath = "P:\Non-Conformance Log\2017"
        strFile = .Range("M1") & " " & .Range("A9").Value
        [COLOR=#FF0000]FilePth= strPath & "\" & strFile[/COLOR]
    
    End With
        
    'THIS SAVES SQD FILE AS FILENAME IN CREATED FOLDER
    ActiveWorkbook.SaveAs Filename:=strPath & strFile
    
    'Create Hyperlink
    rngHLink.Hyperlinks.Add Anchor:=rngHLink, Address:=[COLOR=#FF0000]FilePth[/COLOR]
    
    
CleanUp:
    Set wbSSM = Nothing
    
End Sub

Daverunt, I tried the new code and it did not work. When I clicked on the Hyperlink it said it couldn't find source file
 
Upvote 0
Missed a bit.
Code:
ActiveWorkbook.SaveAs Filename:=FilePth
You can check the hyperlink is right by hovering over it - any errors in the path should be easy to spot.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top