Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 13th, 2005, 05:22 AM
Registered User
 
Join Date: Jul 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copy one sheet to another file

Hi,
I need to develop a VBscript to copy the first and only sheet from file "Origin" to the file "Destination". The sheet I'm copying could already exist in the "Destination" file, so I should delete it before copying. I want to pass as parameters the variables "Origin" (source file), "Destination" (the file where the sheet should be copied) and "SheetToCopy" that contains the sheet name of the sheet to copy. I developed the following code, but it does not work the delete line:

Sub ExcelCopySheet(Origin,Destination,SheetToCopy)
  Dim xlApp
  Dim xlBook1, xlBook2

  Set xlApp = CreateObject("Excel.Application")
  Set xlBook1 = xlApp.Workbooks.Open(Origin)
  Set xlBook2 = xlApp.Workbooks.Open(Destination)

  xlApp.visible = False
  xlApp.DisplayAlerts = False

  xlBook2.Sheets(SheetToCopy).Delete
  xlBook1.Sheets(1).Copy(xlBook2.Sheets(SheetToCopy) )

  xlBook1.save
  xlBook2.save

  xlApp.Quit
  Set xlBook = Nothing
  Set xlApp = Nothing
End Sub

Can you help me with this? Also, I want to sort the "Destination" Sheets, How can I do that?

Thanks,
Salvador Hernandez

 
Old April 13th, 2005, 06:21 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sub mySheetExists()
    On Error Resume Next
    'On Error GoTo myerror

    Workbooks("Book3").Activate

    Worksheets("Origin").Select
    If Err.Number = 0 Then
        Worksheets("Origin").Delete
        Workbooks("Book2").Activate
        Sheets("Origin").Move Before:=Workbooks("Book3").Worksheets(1)
    Else
        Workbooks("Book2").Activate
        Sheets("Origin").Move Before:=Workbooks("Book3").Worksheets(1)
    End If
on error goto 0
Exit Sub
myerror:
    MsgBox (Error$)
on error goto 0

End Sub

change the book 2 and book3 references to your required book names and adapt the code appropriately.

Problem 2 being looked at...

Cheers

Matthew

 
Old April 13th, 2005, 06:45 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

point 2 although this should be another forum question/topic,

Sub SortSheetNames()
Dim mySheet
Dim myArray(100)
Dim myloop As Integer
Dim mytempSheetName As String
Dim mycounter As Integer

Sheets(1).Select
myloop = 0
For Each mySheet In Sheets()
    On Error Resume Next
    myArray(myloop) = ActiveSheet.Name
    ActiveSheet.Next.Select
    myloop = myloop + 1
Next


    Sheets.Add
    mytempSheetName = ActiveSheet.Name
    '"=mysheet()"
    Range("A1").Select
    Do Until myloop = -1
        ActiveCell.Offset(myloop, 0).Value = myArray(myloop)
        myloop = myloop - 1
    Loop

    Range("A1:" & Range("A65000").End(xlUp).Address).Select

    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    Range("a1").Select
    myloop = 0
    Do Until ActiveCell.Offset(myloop, 0).Value = ""
        myArray(myloop) = ActiveCell.Offset(myloop, 0).Value
        myloop = myloop + 1
    Loop
    mycounter = 0
    Do Until myloop = -1

        Sheets(myArray(mycounter)).Move before:=Sheets(mycounter + 1)
        mycounter = mycounter + 1
        myloop = myloop - 1
    Loop

    Sheets(mytempSheetName).Delete
End Sub

if you anticipate having more than 100 sheets (must be crazy man!!!) in your workbook then change the size of the array.

Cheers

Matthew






Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Workbook,Copy Sheet,Move Sheet, Close/Save ptrussell2009 Excel VBA 0 June 13th, 2008 02:28 PM
Copy specific data from one sheet to another yogeshyl Excel VBA 2 May 11th, 2007 09:14 AM
Copy a pivot table fo a new sheet for formatting timmaher Excel VBA 2 February 12th, 2005 06:11 PM
Marcro to copy data from one sheet to another Crupa Excel VBA 1 November 24th, 2004 04:12 AM
Macro to copy data from one file to a 2nd file chadpodsednik Excel VBA 1 October 29th, 2004 10:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.