Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy worksheet to another workbook 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi Guys,

I am trying to copy a worksheet from one workbook to another with the following code

Code:
Private Sub cmdAppend_Click()
    Sheets("Summary").Copy _
    After:=Workbooks("OTIF_2003.xls").Worksheets(Workbooks("OTIF_2003.xls").Worksheets.Count)
    Workbooks("OTIF_2003.xls").Save
    Workbooks("OTIF_2003.xls").Close
    Range("A2").Select
    frmMacros.Hide
End Sub

However I keep getting the error message

Run-time Error 9:Subscript out of range

could anyone help please

Regards

Paul

 
It's OK I have worked it out now, I did not have the workbook open i was trying to copy to

Workbooks("OTIF_2003.xls")

I have now changed my code to this and it works OK

Code:
Private Sub cmdAppend_Click()
    Workbooks.Open Filename:="OTIF_2003.xls"
    Dim wsSummary As Worksheet
    Set wsSummary = ActiveSheet
    Sheets("Summary").Copy _
    After:=Workbooks("OTIF_2003.xls").Worksheets(Workbooks("OTIF_2003.xls").Worksheets.Count)
    Workbooks("OTIF_2003.xls").Save
    Workbooks("OTIF_2003.xls").Close
    Range("A2").Select
    frmMacros.Hide
End Sub
[/code/

However if someone knows a better way feel free to enlighten me 

Regards

Paul
 
why set the sheet object up and then not use it?
Code:
Private Sub cmdAppend_Click()
    dim wb as workbook
    set wb = Workbooks.Open Filename:="OTIF_2003.xls"
    with wb
       Sheets("Summary").Copy After:=.Worksheets(.Worksheets.Count)
       .Save
       .Close
    end with
    set wb = nothing
    Range("A2").Select
    frmMacros.Hide
End Sub

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Hi Geoff,

Thank you very much for the reply and the suggested code, sorry i have not replied earlier but i have been out of circulation for a while.

I do appreciate your help and as always the "guru's" on this site are always happy to help and educate less experienced users such as myself.

I have applied your code to my application and it works beautifully

once again thank you and a "star" is on it's way

Regards

Paul

 
No probs - happy to help

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top