VBA How to copy data in a closed workbook?

Adar123

Board Regular
Joined
Apr 1, 2018
Messages
83
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hi everyone,

I am trying to copy the data from an active sheet to another file. The destination file contains other data which should not be affected, so I am pointing it the location of paste at the first empty row.

For some reason, I get error "object variable or with block variable not set" when reach the point of this line:
VBA Code:
Set wkBk = myApp.Workbooks.Open("DestFile_" & dTod & "." & "xls")

What am I missing?

Extract of VBA code responsible for the action:
VBA Code:
dTod = Format(Date, "yyyymmdd")

Set wkBk = ActiveWorkbook
lastRow = Range("A" & Rows.Count).End(xlUp).Row
wkBk.Sheets("NewData").Range("A1:E" & lastRow).Copy

Set wkBk = myApp.Workbooks.Open("DestFile_" & dTod & "." & "xls")
Set wkSht = wkBk.Sheets("NewData")
wkSht.Activate

lastRow = wkBk.Sheets("NewData").Range("A" & Rows.Count).End(xlUp).Row

Range("A" & lastRow).Select
wkSht.Paste

wkBk.SaveAs "DestFile_" & dTod & "." & "xls"
wkBk.Close
 
VBA Code:
Set Rng =wkBk.Sheets("NewData").Range("A1:E" & lastRow)

This solved the issue. Thanks for the code. It does the job.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top