简体   繁体   中英

Move Excel Sheet from one Workbook to another

I have been trying to figure out how to move (or copy) an Excel sheet from one Workbook to another using Microsoft.Office.Interop.Excel in vb.NET. It seems like Sheets.Move(Before, After) is limited to moving a Sheet to another location within the same Workbook, by using the sheet index as the destination location (where Sheet1 = 1, Sheet2 = 2, and so on).

For example, the following will move Sheet1 to before Sheet3:

Sheets(1).Move(3)

Is there a way I can move (or copy) Sheet1 to another Workbook (that already exists)? I can't figure it out.

This looks like a duplicate from…

C# - How to copy a single Excel worksheet from one workbook to another?

I am aware that this is a C# question, however, the answer provided by NickZucco is a VB solution and appears to work as expected.

In my tests, the following code “moves” a worksheet from SourceWorkbook to DestinationWorkbook

SourceWorkbook.Worksheets(wsheetToMove).Move(After:=DestinationWorkbook.Worksheets(1))

To copy the worksheet…

SourceWorkbook.Worksheets(wsheetToCopy).Copy(After:=DestinationWorkbook.Worksheets(1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM