Solved

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

Posted on 2006-06-29
3
588 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…

777 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