Solved

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

Posted on 2006-06-29
3
592 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

688 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