Copying Sheets from Unopened Workbooks

BrittKnee

Board Regular
Joined
Dec 4, 2017
Messages
82
Hi All. I am trying to copy the sheet containing "EEs" from multiple workbooks in one folder to the active workbook. The code I have runs, but doesn't actually copy the sheets. Any and all help is appreciated!

Code:
Option Explicit

Sub Combine_Dept_Files()

Dim FolderPath, Filename As String
Dim ws As Worksheet
Dim wkb As Workbook
Dim var As Variant

Application.ScreenUpdating = False
Application.DisplayAlerts = False


'Copy Worksheets to Master File

    FolderPath = "S:\9Box_Output\Department Reports\"
    Filename = Dir(FolderPath & "*.xlsm*")
    Do While Filename <> ""
    Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True
        For Each ws In ActiveWorkbook.Sheets
            For Each var In Array("EEs*")
                If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)
            Next var
    Next ws
    Workbooks(Filename).Close
    Filename = Dir()
    Loop

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Do the files contain at least one worksheet which has a name starting with "EEs" (case sensitive!)?
 
Upvote 0
what is this supposed to do?


For Each var In Array("EEs*")
If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)
Next var


surely

If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)

would work without the loop
 
Upvote 0
what is this supposed to do?


For Each var In Array("EEs*")
If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)
Next var


surely

If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)

would work without the loop


sorry it should be something like below
If InStr(ws.Name , "EEs*") > 0 Then ws.Copy After:=ThisWorkbook.Sheets(2)
 
Upvote 0
Do the files contain at least one worksheet which has a name starting with "EEs" (case sensitive!)?
Yup, just double checked to make sure. I even tried naming them all the same name and hard coding the sheet name to see if it had something to do with that. Thanks in advance!
 
Upvote 0
Change this line:
VBA Code:
If InStr(ws.Name, CStr(var)) Then ws.Copy After:=ThisWorkbook.Sheets(2)
to:
VBA Code:
If ws.Name Like CStr(var) Then ws.Copy After:=ThisWorkbook.Sheets(2)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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