Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-06-29
3
Medium Priority
?
595 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

604 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