将一个excel文件内容复制到另一个excel文件的末尾

时间:2011-08-01 07:20:49

标签: excel vba excel-vba excel-2007

如何使用VBA将一个Excel文件复制到另一个excel文件中如何提及两个excel文件的路径?

这是我在网上找到的:

ActiveSheet.Move After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)

这就是我所看到的,但我无法做到这一点,任何人都可以告诉我我的方式吗?

我必须将一个excel文件的所有行和列复制到另一个excel文件的末尾  我该怎么做?

1 个答案:

答案 0 :(得分:4)

如果要将工作表从工作簿移动(或复制)到另一个工作簿,则必须在var 中设置其他工作簿。让我举个例子:

Dim oNewWB as Workbook
Dim oCurWB as Workbook
'Store active workbook in a var
Set oCurWB = ActiveWorkbook
'Open a new workbook
Set oNewWB = Application.Workbooks.Open(<pathToWorkbookHere>)

'Move "Sheet1" from the current workbook to new one
oCurWB.Sheets("Sheet1").Move After:=oNewWB.Sheets(oNewWB.Sheets.Count)

[编辑] Maverik建议的新代码 假设您要将工作簿中的每个工作表复制到新工作表中:

Dim oNewWB as Workbook
Dim oCurWB as Workbook
Dim Sheet as Worksheet
'Store active workbook in a var
Set oCurWB = ActiveWorkbook
'Open a new workbook
Set oNewWB = Application.Workbooks.Open(<pathToWorkbookHere>)

For Each Sheet In oCurWB.Sheets
  'Move "Sheet1" from the current workbook to new one
  Sheet.Move After:=oNewWB.Sheets(oNewWB.Sheets.Count)
Next Sheet

HTH