Open all workbooks to perform an action

camandab

Board Regular
Joined
Feb 10, 2010
Messages
79
I need to a workbook in a specified folder, copy any sheet that contains "Dec" to a new workbook, save and repeat for all worksbooks in a folder

Here is the code I have so far and it only opens all the files in the folder I specified but doesn't take the action to copy the sheets I have specified. Any ideas??


Code:
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook


Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            'Change path to suit
            .LookIn = "C:\...\My Documents\...Recs 2009"
            .FileType = msoFileTypeExcelWorkbooks
            'Optional filter with wildcard
            .Filename = "MOC*.xls"
                If .Execute > 0 Then 'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count 'Loop through all
                        'Open Workbook x and Set a Workbook variable to it
                        Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        

'2.Copy Dec worksheets into new wb

Dim ws As Worksheet
Dim arrShts()
Dim I As Long
 
    For Each ws In Worksheets
        If LCase(ws.Name) Like "*dec*" Then
            ReDim Preserve arrShts(I)
            arrShts(I) = ws.Name
            I = I + 1
        End If
    Next ws
    
    Worksheets(arrShts()).Copy

Next
End If
End With

MsgBox ("Done")


'
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try changing
Code:
Worksheets(arrShts()).Copy

to
Code:
Worksheets(arrShts()).Copy After:=Workbooks(wbResults).Worksheets(Workbooks(wbResults).Worksheets.Count)
                    Windows(wbCodeBook).Activate
 
Upvote 0
Thanks for the quick reply. It still did not copy the sheets to a new workbook. Any other suggestions? Thanks!!
 
Upvote 0
I assume that you have the 2 workbooks wbResults and wbCodeBook open together? Or do they have different names?
 
Upvote 0
You need to use the correct workbook names in the macro instead of wbResults and wbCodeBook or the macro can't rference them.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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