简体   繁体   中英

Copy a specific range from a source worksheet to a target worksheet with different path

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

I need a little help to work this code.

The issue begin here: Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value

So, I have 3 files with different path:

  1. D:\\Tranzit\\2016\\feb\\data_feb.xlsx
  2. D:\\Tranzit\\2016\\\\mar\\data_mar.xlsx
  3. D:\\Tranzit\\2016\\data_final.xlsm

I want to copy from file 1 the range A2:A1000 from "Sheet" Impuls to file 3 in range A2:A1000 from "Sheet" monthly .

and

copy from file 2 the range A2:A1000 from "Sheet" Impuls to file 3 in range B2:B1000 from "Sheet" monthly .

You declared wkbk_feb and wkbk_mar as workbook objects so you need to reference them directly:

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

instead of activating or selecting anything you should always specify the workbook or worksheet. So it should look something like

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

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