Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

open files in all of folders branches in directory

0

Hello,

the code opens files are existed in folder but ignore subfolders and sub subfolders .

so I search for way to expand code to including all file in main folder and folders branches  in directory 

here is the code

Private Sub ListBox1_Click()

   Dim FileRoot As String
   Dim FileName As String
   Const FolderPath = "C:\Users\spp\Desktop\information\"

   With ListBox1
      MsgBox .ListIndex & ": " & .List(.ListIndex, 2)
      FileRoot = .List(.ListIndex, 2)
   End With

   FileName = Dir(objFldr& FileRoot & ".xls*")
   If FileName <> "" Then
      Workbooks.Open FileName:=objFldr& FileName
   Else
      FileName = Dir(objFldr& FileRoot & ".pdf")
      If FileName <> "" Then
         ActiveWorkbook.FollowHyperlink objFldr& FileName
      Else
         MsgBox "File not found for " & FileRoot
      End If
   End If

End Sub

here is my trying 

Private Sub ListBox1_Click()

   Dim FileRoot As String
   Dim FileName As String
    Dim objFldr As Object
    Dim objFSO As Object
    Dim objSubFldr As Object
   Const FolderPath = "C:\Users\pc\Desktop\data\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFldr = objFSO.GetFolder(FolderPath)
    LoopEachFolder objFldr


   With ListBox1
      MsgBox .ListIndex & ": " & .List(.ListIndex, 2)
      FileRoot = .List(.ListIndex, 2)
   End With

   FileName = Dir(FolderPath & FileRoot & ".xls*")
   If FileName <> "" Then
      Workbooks.Open FileName:=FolderPath & FileName
   Else
      FileName = Dir(FolderPath & FileRoot & ".pdf")
      If FileName <> "" Then
         ActiveWorkbook.FollowHyperlink FolderPath & FileName
      Else
         MsgBox "File not found for " & FileRoot
      End If
   End If

End Sub

Function LoopEachFolder(fldFolder As Object)
    Dim Fname As String
    Dim objFldLoop As Object

    Fname = Dir(fldFolder & "\")

    ' List files in this folder
    Do While Fname <> ""

        Fname = Dir()
    Loop

    ' With the subfolders in this folder....
    For Each objFldLoop In fldFolder.subFolders
        '  run this function on each subfolder found
        LoopEachFolder objFldLoop

    Next objFldLoop

End Function

thanks

Answer
Discuss

Discussion

Speed

Further to my (part) Answer below, please attach a representative file, including the UserForm you're using- I'm not sure what ListBox1 contains (presumably the third column contains  a filename but without the file extension).
John_Ru (rep: 6722) Jun 2, '25 at 7:52 am
Please see REVISION #1, 02 June 2025 to my Answer
John_Ru (rep: 6722) Jun 2, '25 at 11:36 am
Add to Discussion

Answers

0
Selected Answer

Hi again Speed.

You can use VBA's FSO (File System Object) to do this.

I think you are good enough at VBA to understand the method (using a function which loops through subfolders and calls itself iteratively)- please see my recent Answer under expanding code to populate files names within subfolders. You could start by opening the file there and clicking the button to select your folder "C:\Users\spp\Desktop\information\".

REVISION #1, 02 June 2025

I don't have your file or the UserForm you use (and source data) but please try this modified code. You'll need to replace the code in your file. I've added comments to help:

Dim i As Long

Sub ListBox1_Click()

    Dim FileRoot As String
    Dim objFldr As Object
    Dim objFSO As Object

    ' initialise "found" counter
    i = 0

    ' state target WITHOUT last \!
    Const FolderPath = "C:\Users\pc\Desktop\data"

    With ListBox1
        MsgBox .ListIndex & ": " & .List(.ListIndex, 2)
        FileRoot = .List(.ListIndex, 2)
    End With

    ' enable use of FSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' get folder
    Set objFldr = objFSO.GetFolder(FolderPath)

    ' pass folder and root to the function
    LoopEachFolder objFldr, FileRoot

    ' quit FSO
    Set objFSO = Nothing

    ' fit results and tell user
    If i <> 0 Then
        MsgBox "Launched " & i & " files"
        Else
        MsgBox "File not found for selection= " & FileRoot
    End If
    'return to this file
    ThisWorkbook.Activate
End Sub


Function LoopEachFolder(fldFolder As Object, fRoot As String)

    Dim objFldLoop As Object

    ' check for xls file in this folder
    Fname = Dir(fldFolder & "\" & fRoot & ".xls*")

    If Fname <> "" Then
        ' open file
        Workbooks.Open FileName:=fldFolder & "\" & Fname
        ' increment the "found" counter
        i = i + 1
     End If

    ' check again for a pdf of that name
    Fname = Dir(fldFolder & "\" & fRoot & ".pdf")
    If Fname <> "" Then
        ' try to open pdf
        ActiveWorkbook.FollowHyperlink fldFolder & "\" & Fname
        ' increment the "found" counter
        i = i + 1
    End If

    ' Then check within the subfolders in this folder...
    For Each objFldLoop In fldFolder.subFolders
        '  run this function on each subfolder found
        LoopEachFolder objFldLoop, fRoot
    Next objFldLoop

End Function

(REVISION #2, 02 June 2025 - corrected the If block which opens a found pdf file.)

('REVISION #3, 04 June 2025 - corrected that If blockto resore increment ation of the "found" counter)

Note that:

  1. the Dim i as Long statement needs to be at the top of the code behind your UserForm
  2. the Function code will open xls and pdf files (of the sdame name), in case both exist
  3. I've assumed that your LIstBox1 contains "root" file names which might be found in the main folder or any deeper subfolders (without the need to list all the files in that ListBox).

I hope this solves your issue. If so, please mark this Answer as Selected.

Discuss

Discussion

Hi John
I edited my post and add my trying , but unfortunately doesn't work .
speed (rep: 46) Jun 2, '25 at 5:06 am
Speed

Your "trying" should be here (not in your Question) really.

I have to go out now (so won't have chance to give more information today) but your effort doesn't work because:

1) the "action" bit (from your original sub) needs to moved from the main sub to the loop within the Function (and altered).

2) your main sub needs to call the new Function, something like:

    Set objFldr = objFSO.GetFolder(FolderPath)
    
    ' call function
    LoopEachFolder objFldr
    
    ' quit FSO
    Set objFSO = Nothing


Sorry I can't help more right now.

 
John_Ru (rep: 6722) Jun 2, '25 at 5:29 am
Please see REVISION #1, 02 June 2025 to my Answer.
John_Ru (rep: 6722) Jun 2, '25 at 11:35 am
thanks John 
when try openning pdf file will show error file can't open
ActiveWorkbook.FollowHyperlink FolderPath & Fname
speed (rep: 46) Jun 2, '25 at 2:47 pm
Oops! That line should read (change in bold):

 
        ' try to open pdf
         ActiveWorkbook.FollowHyperlink fldFolder & "\" & Fname

I just corrected that using REVISION #2, 02 June 2025 to my Answer.  
John_Ru (rep: 6722) Jun 2, '25 at 3:05 pm
I'm not sure why show messagebox         MsgBox "File not found for selection= " & FileRoot  aftrer open file  !
anyway thanks john for your help.
speed (rep: 46) Jun 2, '25 at 3:39 pm
That message should only show if no file is opened.

Thanks for selecting my Answer, Speed. 
John_Ru (rep: 6722) Jun 2, '25 at 3:55 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login