Solved

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

Posted on 2006-06-29
3
579 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
Comment Utility
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
Comment Utility
Thank you very much Yotam. The code you had listed out works perfect.
Thanks again.
0
 
LVL 7

Expert Comment

by:yotamsher
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 74
java constructor error 8 86
Replace a tag with sed 2 41
PHP question(s) about order of output 9 39
This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now