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!
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,237
Office Version
  1. 2013
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
75,813
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
75,813
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
 

Watch MrExcel Video

Forum statistics

Threads
1,108,625
Messages
5,523,970
Members
409,548
Latest member
Buildingbysea

This Week's Hot Topics

Top