VBA to copy worksheets from another workbook

asad

Well-known Member
Joined
Sep 9, 2008
Messages
1,434
Hello All,

i am trying to copy all the worksheets from and external workbook saved in another folder into current workbook. So far, I have this:
Code:
Sub CopyWorksheets()

Dim sh As Worksheet, srcwb As Workbook, trgtwb As Workbook, strng As String
strng = "S:\Planning and scheduling\Rosters\Master Dispatch\Master Dispatch - " & Format(ThisWorkbook.Worksheets("Express").Range("K3").Value, "d mmmm yyyy") & ".xlsm"
[COLOR=#ff0000][B]srcwb = Workbooks(strng)[/B][/COLOR]
trgtwb = ThisWorkbook
For Each sh In Workbooks(srcwb).Worksheets

   sh.Copy After:=trgtwb.Sheets(trgtwb.Sheets.Count)
Next sh


End Sub
The line in red above gets highlighted and the code does not give the intended result. How can I fix this?

Thanks
Asad
 

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.
You need to use the Set command when opening a workbook where you're using a workbook variable like you are i.e.:

Code:
Set srcwb = Workbooks(strng).Open

Robert
 
Upvote 0
Thanks for the reply Robert. But it still did not work. The code now reads:
Code:
Sub CopyWorksheets()

Dim sh As Worksheet, srcwb As Workbook, trgtwb As Workbook, strng As String
strng = "S:\Planning and scheduling\Rosters\Master Dispatch\Master Dispatch - " & Format(ThisWorkbook.Worksheets("Express").Range("K3").Value, "d mmmm yyyy") & ".xlsm"
Set srcwb = Workbooks(strng).Open
trgtwb = ThisWorkbook
For Each sh In Workbooks(srcwb).Worksheets
   sh.Copy After:=trgtwb.Sheets(trgtwb.Sheets.Count)
Next sh


End Sub

May be I did not get what you tried telling me.
 
Upvote 0
Try this.
Code:
Sub CopyWorksheets()

Dim sh As Worksheet, srcwb As Workbook, trgtwb As Workbook, strng As String

    strng = "S:\Planning and scheduling\Rosters\Master Dispatch\Master Dispatch - " & Format(ThisWorkbook.Worksheets("Express").Range("K3").Value, "d mmmm yyyy") & ".xlsm"

    Set srcwb = Workbooks.Open(strng)

    srcwb.Sheets.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

End Sub
 
Upvote 0
Try this.
Code:
Sub CopyWorksheets()

Dim sh As Worksheet, srcwb As Workbook, trgtwb As Workbook, strng As String

    strng = "S:\Planning and scheduling\Rosters\Master Dispatch\Master Dispatch - " & Format(ThisWorkbook.Worksheets("Express").Range("K3").Value, "d mmmm yyyy") & ".xlsm"

    Set srcwb = Workbooks.Open(strng)

    srcwb.Sheets.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

End Sub

One more favour please. After the sheets have been copied, can I get the source workbook to close again. I don't want it to leave it open.
 
Upvote 0
Add this at the end.

Code:
srcwb.Close SaveChanges:=False
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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