Great code just needs a small fix

lakke2120

New Member
Joined
Aug 21, 2014
Messages
32
I found a code that works great for copying multiple workbooks to a master file but it creates a new file and copies to that file and I would like to see if someone could help me with getting it copy to the same workbook that the code is in. Thank you. Here is the complete code:

Sub CopySameSheetFrmWbs()
Dim wbOpen As Workbook
Dim wbNew As Workbook
'Change Path
Const strPath As String = "C:\Users\lap\Desktop\file2\master1\"
Dim strExtension As String

'Comment out the 3 lines below to debug
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next

ChDir strPath
'Change extension
strExtension = Dir("*.xls")

Set wbNew = Workbooks.Add
'Change Path, Name and File Format
wbNew.SaveAs Filename:="C:\Users\lap\Desktop\file2\Book1.xlsm", FileFormat:=xlWorkbookNormal

Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & strExtension)

With wbOpen
.Sheets("totals").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)
wbNew.Sheets(wbNew.Sheets.Count).Name = wbNew.Sheets(wbNew.Sheets.Count).Cells(1, 1)
.Close SaveChanges:=False
End With

strExtension = Dir
Loop

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
On Error GoTo 0
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
instead of this part:
Code:
Set wbNew = Workbooks.Add
     'Change Path, Name and File Format
    wbNew.SaveAs Filename:="C:\Users\lap\Desktop\file2\Book1.xlsm", FileFormat:=xlWorkbookNormal
write:
Code:
Set wbNew = ThisWorkbook

PS. Please note that for frequent VBA users readability improves when the code tags (button with # singn) are used - as above.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,091
Latest member
gaurav_7829

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