Can't Close WorkBook after I Copy Information

Ratriel

New Member
Joined
Dec 9, 2018
Messages
1
Hi guys, Im new in the VBA

What I'm doing is opening a few files to copy the information of each of that to a main file, Im doing this after open a USERFORM with a button in the USERFORM

What I cant do is tell VBA to close the source files, it give a RUNTime Error 1004, Method Close of Object _WorkBook Failed.

If a run the code with a regular button it works, but I need to do with a button in the VBA USERFORM

here is my code:


Private Sub ExtractData_Click()


'Declarando Destino


Dim OrderTool As Workbook
Dim OrderSheet As Worksheet
Set OrderTool = Workbooks(ThisWorkbook.name)
Set OrderSheet = OrderTool.Worksheets("Price Data")


'Declarando Rutas Origen


Dim USpath As String
USpath = "Macintosh HD:Users:sergiocastillo:Dropbox:This Week:CaptainGeorge.USFood.xlsx"


Dim USpathLast As String
USpathLast = "Macintosh HD:Users:sergiocastillo:Dropbox:Last Week:CaptainGeorge.USFood.Last.xlsx"


'Declarando Origenes


Dim usFoodData As Workbook
Dim usFoodLastData As Workbook
'--------------------------------------------
Dim usSheetData As Worksheet
Dim usSheetLastData As Worksheet
'--------------------------------------------
Set usFoodData = Workbooks.Open(USpath)
Set usFoodLastData = Workbooks.Open(USpathLast)
'--------------------------------------------
Set usSheetData = usFoodData.Worksheets("USFood Price List")
Set usSheetLastData = usFoodLastData.Worksheets("UsFood Price List")


lrUS = usFoodData.Sheets("USFood Price List").cells(Rows.Count, 1).End(xlUp).Row
lrUSlast = usFoodLastData.Sheets("USFood Price List").cells(Rows.Count, 1).End(xlUp).Row


usSheetLastData.Range("A1:G" & lrUSlast).Copy Destination:=OrderSheet.Range("h1")
usSheetData.Range("A1:G" & lrUS).Copy Destination:=OrderSheet.Range("a1")


usFoodData.Close SaveChanges:=False
usFoodLastData.Close SaveChanges:=False
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You could try changing:

usFoodData.Close SaveChanges:=False
usFoodLastData.Close SaveChanges:=False

to:
Code:
Workbooks("usFoodData").Close SaveChanges:=False
Workbooks("usFoodLastData").Close SaveChanges:=False

Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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