Consulting

Results 1 to 4 of 4

Thread: open 'xl*' files from sub folders without naming them

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location

    open 'xl*' files from sub folders without naming them

    Hi all.

    I'm using the code below to open all files '.xl*' from a folder (MyFolder).
    However I have many sub folders level1 (about 25 of them) and many sub folders level2 (about 120).
    How could I go with a code to open all '.xl*' files from MyFolder, from SubFolderLevel1 and from SubFolderLevel2 without needing to write the name of all that sub folders?
    using XL 2007

    something like:
    myPath1="C:\MyFolder\"
    myPath2="C:\MyFolder\*\"
    myPath3="C:\MyFolder\*\*\"

    [vba]Sub OpenAllXL()
    Dim myFiles, myPath As String
    myPath = "C:\MyFolder\SubFolderLevel1\SubFolderLevel2\"
    myFiles = Dir(myPath)
    Do While myFiles <> ""
    If myFiles Like "*.xl*" Then
    Workbooks.Open myPath & myFiles
    End If
    myFiles = Dir
    MsgBox myFiles
    Loop
    End Sub
    [/vba]thnks in advance
    Regards
    Osvaldo

  2. #2
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location
    Bump.
    Could someone help me with this?
    Thanks to all.
    Regards
    Osvaldo

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Here is an FSO method. Modify to open in the For loop line with Debug.

    [vba]Sub Test_SearchFiles()
    Dim v As Variant, a() As Variant
    SearchFiles ThisWorkbook.Path, "*.xl*", 0, a(), True
    For Each v In a()
    Debug.Print v
    Next v
    End Sub

    'http://www.ozgrid.com/forum/showthread.php?t=157939
    Private Function SearchFiles(myDir As String _
    , myFileName As String, n As Long, myList() _
    , Optional SearchSub As Boolean = False) As Variant
    Dim fso As Object, myFolder As Object, myFile As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each myFile In fso.getfolder(myDir).Files
    Select Case myFile.Attributes
    Case 2, 4, 6, 34
    Case Else
    If (Not myFile.Name Like "~$*") _
    * (myFile.Path & "\" & myFile.Name <> ThisWorkbook.FullName) _
    * (UCase(myFile.Name) Like UCase(myFileName)) Then
    n = n + 1
    ReDim Preserve myList(1 To 2, 1 To n)
    myList(1, n) = myDir
    myList(2, n) = myFile.Name
    End If
    End Select
    Next
    If SearchSub Then
    For Each myFolder In fso.getfolder(myDir).subfolders
    SearchFiles = SearchFiles(myFolder.Path, myFileName, _
    n, myList, SearchSub)
    Next
    End If
    SearchFiles = IIf(n > 0, myList, CVErr(xlErrRef))
    End Function
    [/vba]

  4. #4
    VBAX Regular
    Joined
    Aug 2011
    Posts
    87
    Location
    Many thanks Ken.
    I'll give it a try later.
    Thnks again.
    Regards
    Osvaldo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •