I'm looking to be able to type a 4 digit value into cells in column M and then when I click on the cells have the relevant PDF open. The PDFs are saved in various subfolders within a folder on a shared drive. The PDF names all end with a unique 4-digit number, however they're all varying lengths and the extra bit before the 4-digit number is different for all of the them. I'm struggling to write a code for this. At the moment I've got a code which will open a PDF, but won't search through subfolders and also requires the initial string of the PDF name to be the same. This is what I've got so far:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim fName As String
Dim fPath As String
Dim fullName As String
'Column with pdf file names, change to suit
Const pdfcol As String = "M"
' Bail out if something is obviously wrong
If Target.Count > 1 Then Exit Sub
If Target.Column <> Columns(pdfcol).Column Then Exit Sub
If Target.Row < 2 Then Exit Sub
If IsEmpty(Target) Then Exit Sub
If Not IsNumeric(Target.Value) Then Exit Sub
' Cancel = True 'Uncomment if using DoubleClick event
'set path to file:
fPath = "O:\01- Calculations\Reports\"
'get file name from cell & adjust for formatting
fName = ActiveCell.Value
fName = Right(fName, 4)
'add the extra 'junk'
fName = "GBF-ZZ-ZZ-CA-S-10-" & fName & ".pdf"
'try to get the full path and name from the folder
fullName = fPath & Dir(fPath & fName)
If fullName = fPath Then
'file not found message
MsgBox "No such filename as " & fName & vbCrLf & _
"In Path " & fPath, vbExclamation
Else
'open it
ActiveWorkbook.FollowHyperlink fullName
End If
End Sub
Can anyone please help me?
Bookmarks