Code to copy same sheet from several workbooks to one not working

mystie

Board Regular
Joined
Feb 15, 2017
Messages
70
Hello everybody!

I am using the following code to copy the same sheet from several workbooks into a new workbook.

Code:
Sub CopySameSheetFrmWbs()
    Dim wbOpen As Workbook
    Dim wbNew As Workbook
     'Change Path
    Const strPath As String = "E:\Proforma Term 1 2017\"
    Dim strExtension As String
     
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    On Error Resume Next
     
    ChDir strPath
     'Change extension
    strExtension = Dir("*.xlsm")
     
    Set wbNew = Workbooks.Add
     'Change Path, Name and File Format
    wbNew.SaveAs Filename:="E:\Proforma Term 1 2017\AllProformaC", FileFormat:=xlWorkbookNormal
     
    Do While strExtension <> ""
        Set wbOpen = Workbooks.Open(strPath & strExtension)
         
        With wbOpen
            
            .Sheets("ProformaC Term 1").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)
            wbNew.Sheets(wbNew.Sheets.Count).Name = wbNew.Sheets(wbNew.Sheets.Count).Cells(5, 4)
            .Close SaveChanges:=False
        End With
         
        strExtension = Dir
    Loop
     
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    On Error GoTo 0
End Sub

However, the only thing it's doing is create the file AllProformaC. That's all. It's not copying the sheets at all.

Any help please?

Thank you.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I've tested your code and it works fine.

Are you sure your source files (containing the sheets you want to copy) contain the .xlsm extension? If not, they will be skipped by the above code.
 
Upvote 0
Yes all the files have .xlsm extension.

Now the source workbooks all have userinterface only protection enabled in the Workbook Open event. Is that a problem?

I did add the code for unprotecting the sheets
Code:
.Sheets("ProformaC Term 1").Unprotect Password:="***"

before the line

Code:
.Sheets("ProformaC Term 1").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)

But still then it didn't work.

Only the worksheets are protected, not the workbooks. If I'm not mistaken, we can copy protected sheets, right?
 
Upvote 0
You can easily find the solution by unprotecting the source workbooks manually then trying to run the macro. If it works, you know it's the protection that's blocking the macro from working correctly.
 
Upvote 0

Forum statistics

Threads
1,216,118
Messages
6,128,939
Members
449,480
Latest member
yesitisasport

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