How to Copy entire Worksheet from Workbook S to Workbook D

K

kris

I want to copy a worksheet from workbook "S.xls" and pase it into Worksheet
of Workbook "D.xls" .I am trying to do something like this,also i can't pass
Workbook Byref.

Sub run_copysheet()
Dim Source, Dest As Workbook

Set Source = Application.Workbooks.Open("c:\S.xls")
Set Dest = Application.Workbooks.Open("c:\D.xls")

Call copysheet(Source, "Sheet1", Source, "Sheet1")

End Sub


Function copysheet(ByVal S1 As Workbook, ByVal SSheetName As String, ByVal
D1 As Workbook, ByVal DSheetName As String)

S1.Worksheets(SSheetName).Activate
Rows("1:30").Select
Selection.Copy

Set D1 = ActiveWorkbook
D1.Worksheets(DSheetName).Activate
Cells.Select
ActiveSheet.Paste


End Function
 
W

WhytheQ

Can you not use something more like:

Workbooks("S.xls").Sheets("Sheet1").Copy
After:=Workbooks("D.xls").Sheets(1)

-seems a lot briefer

J
 
J

Joel

I found two problems. One you are passing "source" twice to the function
copysheet instead passing source and dest. Second you are redefining D1 in
the functtion copysheet. I've commented this statement in the code.

There is no problems with the pasing of parameters (worksheet).



Sub run_copysheet()
Dim Source, Dest As Workbook

Set Source = Application.Workbooks.Open("c:\temp\S.xls")
Set Dest = Application.Workbooks.Open("c:\temp\D.xls")

Call copysheet(Source, "Sheet1", Dest, "Sheet1")

End Sub


Function copysheet(ByVal S1 As Workbook, _
ByVal SSheetName As String, ByVal _
D1 As Workbook, ByVal DSheetName As String)

S1.Worksheets(SSheetName).Activate
Rows("1:30").Select
Selection.Copy

'Set D1 = ActiveWorkbook
D1.Worksheets(DSheetName).Activate
Cells.Select
ActiveSheet.Paste


End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top