+ Reply to Thread
Results 1 to 7 of 7

Macro to open a workbook from an unknown file path

Hybrid View

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Macro to open a workbook from an unknown file path

    Please can someone provide me with a macro to do the following:

    Find a file (say test.xls) from a unknown file path (say in a subdirectory on C:/ drive)

    Thanks

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to open a workbook from an unknown file path

    Sub FindInSubfoldersFileOpen()
       Dim StrFile As String, objFSO, destRow As Long
       Dim mainFolder, mySubFolder
       mFolder = Range("B2").Value ' initial folder
       fname = Range("B3").Value ' file name
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set mainFolder = objFSO.GetFolder(mFolder)
       StrFile = Dir(mFolder & "\" & fname)
       If StrFile <> "" Then
          Workbooks.Open mFolder & "\" & StrFile
       Else
         For Each mySubFolder In mainFolder.SubFolders
           StrFile = Dir(mySubFolder & "\" & fname)
           If StrFile <> "" Then
              Workbooks.Open mySubFolder & "\" & StrFile
              Exit For
           End If
         Next
       End If
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to open a workbook from an unknown file path

    Patel45. Still having trouble with this one.

    Macro seems to stop on Set objFSO = CreateObject("Scripting.FileSystemObject")

    Any suggestions why??

  4. #4
    Registered User
    Join Date
    11-27-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to open a workbook from an unknown file path

    Ahhh finally got it working. Seems issue is that it can search one subdirectory deep. Is it possible to modify it to search through say 4 tiers for subdirectories? Thanks for your help.

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to open a workbook from an unknown file path

    Sub FindInSubfolders() ' main, call SubFoldersFind
       Dim StrFile As String, objFSO, destRow As Long, fname As String
       Dim mainFolder, mySubFolder
       mFolder = Range("B2").Value
       fname = Range("B3").Value
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set mainFolder = objFSO.GetFolder(mFolder)
       StrFile = Dir(mFolder & "\" & fname)
       If StrFile <> "" Then
          Workbooks.Open mFolder & "\" & StrFile
       Else
          SubFoldersScan OfFolder:=mainFolder, fname:=fname
       End If
    End Sub
       
    Sub SubFoldersScan(OfFolder As Variant, fname As String) 
        Dim SubFolder
        For Each SubFolder In OfFolder.SubFolders
           StrFile = Dir(SubFolder & "\" & fname)
           If StrFile <> "" Then
              Workbooks.Open SubFolder & "\" & StrFile
              Exit For
           End If
           SubFoldersScan OfFolder:=SubFolder, fname:=fname
        Next SubFolder
    End Sub
    Last edited by patel45; 11-30-2012 at 07:16 AM.

  6. #6
    Registered User
    Join Date
    11-27-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to open a workbook from an unknown file path

    Thanks very Patel45. That works great now. Is it possible to add the following step too.


    Say cell B4 in the original spreadsheet (where this macro is located) is the name of a macro in the newly opened spreadsheet. I need to run this macro and then copy data (say cell B5) from the original spread sheet into the newly opened spreadsheet (say into cell A1)

    Hope that makes sense. Thanks again

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to open a workbook from an unknown file path

    obviously not tested
    Sub FindInSubfolders() ' main, call SubFoldersFind
       Dim StrFile As String, objFSO, destRow As Long, fname As String
       Dim mainFolder, mySubFolder
       mFolder = Range("B2").Value
       fname = Range("B3").Value
       macroname = Range("B4").text
       mdata = Range("B5").text   
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       Set mainFolder = objFSO.GetFolder(mFolder)
       StrFile = Dir(mFolder & "\" & fname)
       If StrFile <> "" Then
          Workbooks.Open mFolder & "\" & StrFile
       Else
          SubFoldersScan OfFolder:=mainFolder, fname:=fname
       End If
       Range("A1").value = mdata
       nn = ActiveWorkbook.Name
       Application.Run "'" & nn & "'!" & macroname
    End Sub
       
    Sub SubFoldersScan(OfFolder As Variant, fname As String) 
        Dim SubFolder
        For Each SubFolder In OfFolder.SubFolders
           StrFile = Dir(SubFolder & "\" & fname)
           If StrFile <> "" Then
              Workbooks.Open SubFolder & "\" & StrFile
              Exit For
           End If
           SubFoldersScan OfFolder:=SubFolder, fname:=fname
        Next SubFolder
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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