Results 1 to 1 of 1

VBA to search through subfolders and open PDF based on cell value

Threaded View

  1. #1
    Registered User
    Join Date
    08-10-2017
    Location
    London
    MS-Off Ver
    2010
    Posts
    13

    VBA to search through subfolders and open PDF based on cell value

    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?
    Last edited by stormhayward; 08-16-2017 at 05:58 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] New To VBA - How Do I Search Subfolders Based On Column Data?
    By portokie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-03-2016, 05:00 PM
  2. open webpage and search based on cell value
    By tonyworkssmarter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-17-2015, 02:32 PM
  3. [SOLVED] FSO to search until subfolders
    By zhaype in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-18-2013, 03:29 AM
  4. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  5. Workbooks.open to search in a certain path and its subfolders
    By Macko07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2012, 08:12 AM
  6. Replies: 2
    Last Post: 03-26-2012, 07:12 PM
  7. Open all files in subfolders
    By linglc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-19-2006, 05:40 AM

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