Closing workbook variable

tris070891

New Member
Joined
Sep 10, 2014
Messages
12
I have successfully created a macro that opens the latest modified excel file in a folder. However I am unable to close the workbook after I open it. This is what the successful macro looks like:

Dim strFolder As String
Dim strFile As String
Dim latestFile As String
Dim dtLast As Date


' assign variables
strFolder = "S:\Active Projects\8450 - Woolworths EDR Brand Tracker Research\8450 - Project Documents\8450 - Fieldwork\8450 - Sample\8450 - Project History\"
strFile = Dir(strFolder & "\*.*", vbNormal)


' loop through files to find latest modified date
Do While strFile <> ""
If FileDateTime(strFolder & strFile) > dtLast Then
dtLast = FileDateTime(strFolder & strFile)
latestFile = strFolder & strFile
End If
strFile = Dir
Loop

Workbooks.Open (latestFile)


So when workbook opens this is the code that I have used in an attempt to close it

Workbooks.Close (latestFile)

This doesn't work... Can anyone help me out?

Cheers!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,274
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Workbooks.Close (latestFile)
Is this being used in the workbook_open event ?
If not, how are you firing the code to execute Workbooks.close
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
Try using a variable to refer to the workbook.
Code:
Set wbLatest = Workbooks.Close (latestFile) 

' do stuff

' close workbook 
wbLatest.Close SaveChanged:=False
 

tris070891

New Member
Joined
Sep 10, 2014
Messages
12

ADVERTISEMENT

I have tried all of these suggestions and none so far have worked..... I get this message ""Wrong number of arguments or invalid property assignment" with the close workbook code highlighted
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
There were a few typos in the code I posted, this id how it should read.
Code:
Set wbLatest = Workbooks.Open(latestFile) 

' do stuff

' close workbook 
wbLatest.Close SaveChanges:=False
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,600
Messages
5,832,651
Members
430,151
Latest member
Kacbear

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
Top