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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,224,746
Messages
6,180,705
Members
452,994
Latest member
Janick

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