Solved

How can you move excel worksheets across different excel work books?

Posted on 2006-06-29
3
590 Views
Last Modified: 2008-01-09
Can anyone let me know if there is a way to add one excel workbook’s  excel worksheet  to another excel workbook.
The following is the code I have done. But I am getting an error on the following line
xlApp.Sheets.Move(, xlSheetBranchTemp)

This is the code I am using ---------------->

        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheetTemp As Excel.Worksheet
        xlApp = CreateObject("Excel.Application")
        xlApp.DisplayAlerts = False
        xlBook = xlApp.Workbooks.Open("c:\Test4.xls")


        Dim xlAppBranch As New Excel.Application
        Dim xlBookBranch As Excel.Workbook
        Dim xlSheetBranchTemp As Excel.Worksheet
        xlBookBranch = xlAppBranch.Workbooks.Open("c:\Test5.xls")

        xlSheetBranchTemp = xlAppBranch.ActiveWorkbook.Worksheets(1)
        xlSheetBranchTemp.Select()

        xlApp.Sheets.Move(, xlSheetBranchTemp)

        xlApp.ActiveWorkbook.Worksheets.Item(1).name = "Summary"
        xlApp.ActiveWorkbook.Worksheets.Item(2).name = "Branch1"

        xlBookBranch.Close()
        xlAppBranch = Nothing
        xlBook.SaveAs("c:\Test4.xls")
        xlBook.Close()
        xlApp = Nothing


0
Comment
Question by:courchjo
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
yotamsher earned 500 total points
ID: 17014170
Hey courchjo

unfortunately I don't have Visual basic installed on this computer, so I had to do my tests with vba inside another excel workbook, but don't worry we have the answer.

the method you are trying to use  (Sheets.Move), is meant to change the order of existing sheets in a workbook.
the way to copy sheets from one book to the other, is through woksheet.copy.

the following code is what I came up with in vba, in excel, the main difference here, is that I have the Application object in hand.

one more change that I took, is using a workbook object already in my hand instead of
ActiveWorkbook

I hope this helps

Yotam


       'Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheetTemp As Excel.Worksheet
        'xlApp = CreateObject("Excel.Application")
             
        Application.DisplayAlerts = False
        Set xlBook = Application.Workbooks.Open("c:\temp\Test4.xls")
        Set xlSheetTemp = xlBook.Worksheets(1)
       


'        Dim xlAppBranch As New Excel.Application
        Dim xlBookBranch As Excel.Workbook
        Dim xlSheetBranchTemp As Excel.Worksheet
        Set xlBookBranch = Application.Workbooks.Open("c:\temp\Test5.xls")

        Set xlSheetBranchTemp = xlBookBranch.Worksheets(1)
        xlSheetBranchTemp.Select

        'xlApp.Sheets.Move , xlSheetBranchTemp
        xlSheetBranchTemp.Copy , xlSheetTemp
       
        xlBook.Worksheets(1).Name = "Summary"
       
        xlBook.Worksheets(2).Name = "Branch1"

        xlBookBranch.Close
       
        Set xlAppBranch = Nothing
        xlBook.SaveAs ("c:\temp\Test4.xls")
        xlBook.Close
        Set xlApp = Nothing
0
 

Author Comment

by:courchjo
ID: 17020668
Thank you very much Yotam. The code you had listed out works perfect.
Thanks again.
0
 
LVL 7

Expert Comment

by:yotamsher
ID: 17022703
Hey courchjo

You are welcome
one more comment
if you use vb.net, Microsft has wrapped excell for you as an assembly instead of Com, so you can use  
                              Microsoft.Office.Interop.Excel            

have a nice day

           Yotam
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question