Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy excel worksheets from different books in VB.NET 1

Status
Not open for further replies.

jonx

Programmer
Sep 2, 2003
34
0
0
US
Hello, I have two workbooks, WB1.xls and WB2.xls and
I would like to copy the worksheet from the first workbook to the first sheet on the second workbook in VB.net

How would I go about doing this>?

Thanks
 
I have some code here, but for some reason I can't get it to work properly.

tempExcel.ActiveWorkbook.Sheets(1).Copy( _
After:=oExcel.ActiveWorkbook.Sheets(3))

I could copy from one worksheet to another worksheet on the same workbook, but if I execute the code above in which I am copying one workbook to another different workbook, I get an error saying "Copy method of Worksheet class failed."

I would like to copy from tempExcel workbook, worksheet 1 to oExcel workbook, worksheet 1.
 
I've created a macro for that job and here what I’ve got:
Sheets("Sheet1").Copy After:=Workbooks("Book2.xls").Sheets(3)
I hope it's helpful
 
Thanks but I needed the code in Vb .net How would i do this in .NET? The MSDN library says that there is a built in method in the worksheet class:

ThisApplication.ActiveWorkbook.Sheets(1).Copy( _
After:=ThisApplication.ActiveWorkbook.Sheets(3))

but it won't allow me to copy and paste from different workbooks because it tells me that the worksheet class failed
 
Did the both workbooks are opened. Open them in code and then try to copy the sheets. The name of excel file is't relative path to the file, it is the name of active workbook.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top