将特定范围从源工作表复制到具有不同路径的目标工作表

时间:2016-09-16 08:41:28

标签: vba excel-vba excel

Dim path_feb As String
Dim path_mar As String
Dim wkbk_feb As Workbook
Dim wkbk_mar As Workbook




path_feb = "D:\Tranzit\2016\feb\data_feb.xlsx"
Set wkbk_feb = Workbooks.Open(path_feb)


path_mar = "D:\Tranzit\2016\mar\data_mar.xlsx"
Set wkbk_mar = Workbooks.Open(path_mar)




Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value
Worksheets("monthly").Range("B2:B1000").Value = Windows("wkbk_mar").Worksheet("impuls").Range("A2:A1000").Value

End Sub

我需要一些帮助才能使用此代码。

问题从这里开始: Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value

所以,我有3个不同路径的文件:

  1. D:\Tranzit\2016\feb\data_feb.xlsx
  2. D:\Tranzit\2016\\mar\data_mar.xlsx
  3. D:\Tranzit\2016\data_final.xlsm
  4. 我想从文件1中复制范围A2:A1000来自" Sheet" Impuls来自" Sheet"范围A2:A1000的档案3 monthly

    从文件2复制范围A2:A1000来自" Sheet" Impuls来自" Sheet"范围B2:B1000的档案3 monthly

1 个答案:

答案 0 :(得分:1)

您将wkbk_febwkbk_mar声明为工作簿对象,因此您需要直接引用它们:

wkbk_feb.Worksheets("impuls")....

而不是激活或选择任何内容,您应该始终指定工作簿或工作表。所以它应该看起来像

wkbk_total.Worksheets("monthly")... = wkbk_feb.Worksheets("impuls")....