0

我有这段代码可以从 VBA/Access 中的工作簿复制工作表到另一个工作簿/文件。

Dim File1 as String
Dim File2 as String
File1 = "D:\File1.xls"
File2 = "D:\File2.xls"

Windows(File1).Activate
Sheets("Name of Sheet").Select
Sheets("Name of Sheet").Copy Before:=Workbooks(File2).Sheets("Name of Target Sheet")

这是行不通的。我需要在后台复制。还要禁用任何宏。

  1. 我怎样才能让它工作?
  2. 我可以给出索引而不是“工作表名称”吗?
  3. 我可以给一个索引数组复制到第二个工作簿吗?
4

2 回答 2

2

如果您在 MS Access 中运行,则需要以下几行:

Dim CopyFrom As Object
Dim CopyTo As Object ''Early binding: Workbook
Dim CopyThis As Object
Dim xl As Object ''Early binding: New Excel.Application

''Late binding
Set xl = CreateObject("Excel.Application")
xl.Visible = True

''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
Set CopyFrom = xl.Workbooks.Open("z:\docs\From.xls")
Set CopyThis = CopyFrom.Sheets(1) ''Sheet number 1
Set CopyTo = xl.Workbooks.Open("z:\docs\To.xls")
CopyThis.Copy After:=CopyTo.Sheets(CopyTo.Sheets.Count)

CopyFrom.Close False
于 2012-07-20T11:53:24.843 回答
0

您需要先打开工作簿。

Dim File1 As String
Dim File2 As String
File1 = "C:\Path\to\file\Book13.xlsx"
File2 = "C:\Path\to\file\Book2.xlsx"

Workbooks.Open Filename:=File2
Workbooks.Open Filename:=File1
ActiveWorkbook.Worksheets("Sheet2").Select
Sheets("Sheet1").Copy Before:=Workbooks("Book2").Sheets(1)

您可以将工作表名称或索引与 Sheets 对象一起使用。要将多个工作表复制到另一个工作簿中,您可以将数组传递到循环中。

于 2012-07-20T11:58:49.440 回答