简体   繁体   中英

vba excel copy values from one workbook to another?

I am trying to copy my values from when workbook to another workbook and paste the values starting from row 200 on the target workbook.

I am new to vba so just been trying to use the following code to do what I need to do but it won't work, ideally I want the copy and paste to take place without the target workbook being opened.

Can someone please show me how I can get my code to do what I need? Thanks in advance,

Dim ws10 As Worksheet, ws12 As Worksheet
Dim DestRow As Long
Set ws10 = Workbooks("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\Supplier SetUps & Amendments.xls").Sheets("Statistics")
Set ws12 = Workbooks("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\Supplier Tracking & Management.xls").Sheets("SupplierTracking")
DestRow = ws12.Cells(Rows.Count, "B").End(xlUp).Row + 1
ws10.Range("A" & ActiveCell.Row).Copy
ws12.Range("B" & DestRow).PasteSpecial xlPasteValuesAndNumberFormats 

To turn the destination workbook not visible i recommend you to create another object to open it. But you can't PasteSpecial in another object so you need to just paste it.

Dim ws10 As Worksheet, ws12 As Worksheet
Dim DestRow As Long
Dim objExcel
Dim objWkb

Set objExcel = CreateObject("Excel.Application")
Set objWkb = objExcel.Workbooks.Open("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\Supplier Tracking & Management.xls")

Set ws10 = Workbooks("\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\Supplier SetUps & Amendments.xls").Sheets("Statistics")
Set ws12 = objWkb.Sheets("SupplierTracking")
DestRow = ws12.Cells(Rows.Count, "B").End(xlUp).Row + 1
ws10.Range("A" & ActiveCell.Row).Copy
ws12.Range("B" & DestRow).Paste

Don't forget to save and quit the workbook before the end of your code.

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