Copy worksheets from unopened workbook into one

BrittKnee

Board Regular
Joined
Dec 4, 2017
Messages
82
Hi All,

I am trying to copy worksheets from files that are all in the same folder (current folder of the "main" file). This folder name changes, so I don't want to hardcode the file path into the code. I have the below code, but it isn't doing anything. There are no debug/errors, it just completes without updating. I've used this code in the path with no issue. Any and all help is appreciated,

Code:
Option Explicit

Sub Consolidate_WBS()

Dim FolderPath, Filename, FolderName As String
Dim ws, sht, trg, wsht, wks, wrks As Worksheet
Dim wrk, wb As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Copy worksheets to file

FolderPath = Application.ActiveWorkbook.Path
Filename = Dir(FolderPath & "*.xlsx*")
Do While Filename <> ""
 Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True
 For Each ws In ActiveWorkbook.Sheets
 ws.Copy After:=ThisWorkbook.Sheets("Detail")
 Next ws
 Workbooks(Filename).Close
 Filename = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I was able to resolve this by adding & "\" to the FolderPath. See the below code:

Code:
Option Explicit

Sub Consolidate_WBS()

Dim FolderPath, Filename, FolderName As String
Dim ws, sht, trg, wsht, wks, wrks As Worksheet
Dim wrk, wb As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Copy worksheets to file

FolderPath = Application.ActiveWorkbook.Path & "\" 
Do While Filename <> ""
 Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True
 For Each ws In ActiveWorkbook.Sheets
 ws.Copy After:=ThisWorkbook.Sheets("Detail")
 Next ws
 Workbooks(Filename).Close
 Filename = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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