如何在Excel工作簿之间复制和粘贴工作表?

问题描述:

如果使用VBA打开两个Excel应用程序,如何将工作表从一个Excel应用程序(1)转移到另一个Excel应用程序(2)?

How do you transfer a worksheet from one excel app(1) to another(2) if you have two excel apps open using VBA?

问题是,程序员使用JavaScript,并且当您单击将Web数据传输到xl工作簿的按钮时,它将打开一个新的Excel应用程序.

The problem is, the programmer uses JavaScript, and when you click on the button that transfers the web data to a xl workbook, it opens a new Excel app.

我知道部分代码将是:

Workbooks.Add
ActiveSheet.Paste    
' Once I returned to the original , i.e. excel app(1).

未经测试,但类似于:

Dim sourceSheet As Worksheet
Dim destSheet As Worksheet

'' copy from the source
Workbooks.Open Filename:="c:\source.xls"
Set sourceSheet = Worksheets("source")
sourceSheet.Activate
sourceSheet.Cells.Select
Selection.Copy

'' paste to the destination
Workbooks.Open Filename:="c:\destination.xls"
Set destSheet = Worksheets("dest")
destSheet.Activate
destSheet.Cells.Select
destSheet.Paste

'' save & close
ActiveWorkbook.Save
ActiveWorkbook.Close

请注意,这假设目标表已经存在.否则,创建一个很容易.

Note that this assumes the destination sheet already exists. It's pretty easy to create one if it doesn't.