Copy worksheet from workbook in multiple sub-folders

WWII_Buff

Board Regular
Joined
Nov 13, 2017
Messages
88
Hey guys! I found this vba code posted by Alan Murray and edited for what I need it to do but when I run it, I get the windows blue doughnut for a second and that's it. It doesn't error or hang, it simply does nothing.

The task I am trying to accomplish is to:


1. Go into each folder and sub-folder and find a workbook called "*_Budget_*.xlsm".
2. When said file is found, open it and copy only the "BUDGET" worksheet to an existing workbook named "ALL SITES.xls" (...this houses the macro).
3. Since every worksheet is named "BUDGET", I would like to rename each worksheet to whatever is in its own cell "C1"

Any help will be appreciated.


Code:
Sub LoopSubfoldersAndFiles()    Dim fso As Object
    Dim folder As Object
    Dim subfolders As Object
    Dim SourceFile As String
    Dim wb As Workbook
    Dim CurrFile As Object


With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("C:\BUDGET\TEMPLATES\Go_Live")
    Set subfolders = folder.subfolders
    SourceFile = "*_Budget_*.xlsm"
    
    For Each subfolders In subfolders
    
    Set CurrFile = subfolders.Files
        
        For Each CurrFile In CurrFile
            If CurrFile.Name = SourceFile Then
                Set wb = Workbooks.Open(subfolders.Path & "\" & SourceFile)
                    Sheets("BUDGET").Copy After:=Workbooks("ALL SITES.xlsm").Sheets("TOTAL BUDGET")
                    ActiveSheet.[C1] = ActiveSheet.Name
                wb.Close SaveChanges:=False
            End If
        Next
       
    Next
     
    Set fso = Nothing
    Set folder = Nothing
    Set subfolders = Nothing


With Application
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With


End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top