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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Workbooks.Close (latestFile)
Is this being used in the workbook_open event ?
If not, how are you firing the code to execute Workbooks.close
 
Upvote 0
Try using a variable to refer to the workbook.
Code:
Set wbLatest = Workbooks.Close (latestFile) 

' do stuff

' close workbook 
wbLatest.Close SaveChanged:=False
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,218,796
Messages
6,144,529
Members
450,551
Latest member
Mouse7681

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