VBA to Auto Hyperlink a .pdf file from a folder based on cell value

djones42

New Member
Joined
Aug 10, 2016
Messages
9
I'm looking for some code or some detailed help as I have really little to no experience with visual basic.

So here's what I'm trying to achieve:

I have a spreadsheet of a test log which features test numbers in column A, and other important info such as test date, description etc. in the following columns. These test numbers are all 4 digit numbers, "1234." There's really no finite end to the amount of test numbers, the list is always growing.

All of these test numbers have a scanned PDF of the test file in a certain folder. The PDF filenames are either the exact test number or the test number with some additional test details, so either "1234" or "1234 example test info."

I want to automatically add hyperlinks of these PDF's to either one cell at a time or all cells in column A. However, since some filenames are not exact matches to the cell's in column A, the code should ignore the extra text. I feel in would maybe make the most sense if the code could some how only look at the first 4 digits of the filename as that is always going to be the test number so would match what value was in the cell(s).
 
I am not sure if it will work or not, but try to change

Code:
[COLOR=#333333]subFolder = myPath & Range("A" & i).Value & "\"[/COLOR]

to
Code:
[COLOR=#333333]subFolder = myPath & Range("A" & i).Value & "[/COLOR][COLOR=#ff0000][B]*[/B][/COLOR][COLOR=#333333]\"[/COLOR]

See if that allows it
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I had already given that a go actually and I guess it reads it wit the asterisk as part of the directory/folder name and gives me an error. If you have any other epiphanies on how to make that work feel free to let me know otherwise I'd say don't worry about it, you've certainly helped me out enough.
 
Upvote 0
hello!
I am searching this type of code for days now. and its working fine. My problem is I have multiple duplicate files and what I need to be linked is the biggest file. pls help.
 
Upvote 0
How can you have multiple duplicate files? The file names must have some sort of different identifier as you can not save a file with the same name as a currently saved file...
 
Upvote 0
Try this, if you are not aware of where to use this or how to execute it let me know, will try to help

Code:
Sub AddHypaerlinks()

Dim lastRow As Long
Dim myPath As String, fileName As String


myPath = "C:\Users\dchaney\Documents\" 'SET TO WHERE THE FILES ARE LOCATED
lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow

    fileName = myPath & Range("A" & i).Value & "*.pdf"
    
    If Len(Dir(fileName)) <> 0 Then 'IF THE FILE EXISTS THEN
    
        ActiveSheet.Hyperlinks.Add Range("A" & i), myPath & Dir(fileName)
    
    End If
    
Next

End Sub

i have a very similar requirement. i tried this and it almost works for me aswell.
but i have a few issues
1. my file names contain "." instead of "/" as widows doesnt allow / to be in a file name.
ex: file name INV.17.163 and cell content INV/17/163
2. empty cells are linked to a random file.

can you help me modifying the code so it fits my purpose
 
Upvote 0
the below code is good but i need one for exact cell value for example pdf file 1000 in this code come to cell va;ue 1 and 10 and 100 and 1000 i need code to cell value 1000 only


Code:
Sub AddHypaerlinks()

Dim lastRow As Long
Dim myPath As String, fileName As String


myPath = "C:\Users\dchaney\Documents\" 'SET TO WHERE THE FILES ARE LOCATED
lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow

    fileName = myPath & Range("A" & i).Value & "*.pdf"
    
    If Len(Dir(fileName)) <> 0 Then 'IF THE FILE EXISTS THEN
    
        ActiveSheet.Hyperlinks.Add Range("A" & i), myPath & Dir(fileName)
    
    End If
    
Next

End Sub
[/QUOTE]
 
Upvote 0
@dchaney

This code works great!!
Can you please let me know what coding language is used in here??

Also I have only one thing to clarify or customize in this formula that say if I have files named abc, abc Rev.1 and abc Rev.2

What file will be picked in the the excel-cell with file name abc???
I would actually like to link the excel-cell to the last revision i.e. abc rev.2 in this case. Similarly I have several files with more than 1 revisions. Is it possible to implement in this formula.

I would highly appreciate a response to this.

Many thanks!
 
Upvote 0
Hi there, I know a lot of time has passed since this was initially posted but it seems to fit my need very closely. I copied your code and pasted it in VB but when I try to run the code, nothing happens. No window, no message, nothing.
What am I missing? Please advise.
Thank you!
 
Upvote 0
Hi there, I know a lot of time has passed since this was initially posted but it seems to fit my need very closely. I copied your code and pasted it in VB but when I try to run the code, nothing happens. No window, no message, nothing.
What am I missing? Please advise.
Thank you!


Did you copy the directory and file locations where it says in green text?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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