+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    06-03-2009
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Walking Through Many Subdirectories, Many Workbooks, Many Worksheets

    Hello, I need to copy a specific sheet (CONSTANT gPA) from multiple workbooks in multiple subdirectories and paste (special) that sheet to the workbook where my code is located (which is at the root, C:\2010 Budget\).

    The code below walks through these subdirectories well enough, but it does not "Open" any of the workbooks. Here is the line/code for opening the workbook:

    Code:
    Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
    To give more context here is a larger code snippet:

    Code:
    For i = 1 To 50
            If FileOrDirExists("C:\2010 BUDGET\" & stateHold(i) & Application.PathSeparator) Then
                With Application.FileSearch
                    .NewSearch
                    .LookIn = "C:\2010 BUDGET\" & stateHold(i) & Application.PathSeparator
                    .FileType = msoFileTypeExcelWorkbooks
                    .Filename = "*.xls"
                    If .Execute() > 0 Then 'Workbooks in folder
                        For lCount = 1 To .FoundFiles.Count ' Loop through all.
                            Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                                If IsNumeric(Mid(wbResults.Name, 3, 4)) Then
                                    For Each wks In Worksheets
                                        Select Case wks.Name
                                        Case gPA
                                            wks.Unprotect
                                            wks.Copy
                                            Workbooks("2010 BUDGET TEMPLATE-ACCT-TEST.xls").Activate
                                            Sheets.Add.Name = wbResults.Name & " - GPA"
                                            Sheets(wbResults.Name & " - GPA").Select
                                            Selection.PasteSpecial Paste:=xlValues
                                            Application.CutCopyMode = False
                                        End Select
                                    Next wks
                                End If
                                Workbooks(wbResults.Name).Activate
                                wbResults.Close SaveChanges:=True
                        Next lCount
                    End If
                End With
            End If
        Next i
    I am using Excel '07 on Vista, and I have tried to run this macro as both a '07 and '03 xls. Where do you think I am going wrong here?

    Thanks in advance.

  2. #2
    Valued Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Eastlake Ohio
    MS-Off Ver
    2010
    Posts
    1,000

    Re: Walking Through Many Subdirectories, Many Workbooks, Many Worksheets

    You stated you are also working in Excel 2007, how many files use the 2007 suffix such as ".xlsx" or ".xlxm"?

    Your code wont find those.
    Code:
    Filename = "*.xls"
    Regards

    Rick
    Win7, Office 2010

  3. #3
    Registered User
    Join Date
    06-03-2009
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Walking Through Many Subdirectories, Many Workbooks, Many Worksheets

    are all .XLS...should've mentioned that initially...

    Thanks!

  4. #4
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,137

    Re: Walking Through Many Subdirectories, Many Workbooks, Many Worksheets

    FileSearch is not supported in Excel 2007.

    If you're doing it in 2003, you should be able to debug to see how many files are returned by the search
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,498

    Re: Walking Through Many Subdirectories, Many Workbooks, Many Worksheets

    Hello scm24,

    This macro uses the File System Object which is supported in Excel 2003 and 2007. It checks for either ".xls" or ".xlsx" file types.
    Code:
    Sub CopyFiles()
    
      Dim DstWks As Worksheet
      Dim File As Object
      Dim Folder As Object
      Dim FolderPath As String
      Dim FSO As Object
      Dim SubFolders As Object
        
        FolderPath = "C:\2010 BUDGET"
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
          Set SubFolders = FSO.GetFolder(FolderPath).SubFolders
            For Each Folder In SubFolders
              For Each File In Folder.Files
                If File Like "*.xls" Or File Like "*.xlsx" Then
                   Set wbResults = Workbooks.Open(Filename:=File.Path & "\" & File.Name, UpdateLinkes:=False)
                   If IsNumeric(Mid(wbResults.Name, 3, 4)) Then
                      With Workbooks("2010 BUDGET TEMPLATE-ACCT-TEST.xls")
                        Set DstWks = .Worksheets(.Worksheets.Count)
                          With wbResults.Worksheets("gPA")
                            .Unprotect
                              .Copy After:=DstWks
                            .Protect
                          End With
                        DstWks.Name = wbResults.Name & " - GPA"
                      End With
                   End If
                   wbResults.Close SaveChanges:=True
                End If
              Next File
            Next Folder
            
        Set FSO = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

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

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.2.0