Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to open file

How would I write a macro to open the most recent excel or xml file within a
folder? I have file names that start with the same first five letters, but
the end of the file name changes according to the date of the file. On a
daily basis, my macro needs to open the most recent file created in a
specified folder.
--
Thank you, Jodie
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default Macro to open file

Hi Jodie

Give this a try. It will open the most recent Excel file in a
specified folder. Change path to suit.

Take care

Marcus

Option Explicit
Const XLFILE_FOLDER = "C:\" 'Change to suit

Sub RecentXLFile()
Dim fs As Object
Dim objFolder As Object
Dim objFile As Object
Dim myFile As Object
Dim myDate As Date
myDate = DateValue("1/1/1900")

Set fs = CreateObject("Scripting.FileSystemObject")
Set objFolder = fs.GetFolder(XLFILE_FOLDER)

For Each objFile In objFolder.Files
If InStr(objFile.Name, ".xls") 0 Then 'xml can go here too.
If objFile.DateLastModified myDate Then
myDate = objFile.DateCreated
Set myFile = objFile
End If
End If
Next

If Not myFile Is Nothing Then
Workbooks.Open Filename:=myFile
End If

End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Macro to open file

This is great!
--
Thank you, Jodie


"marcus" wrote:

Hi Jodie

Give this a try. It will open the most recent Excel file in a
specified folder. Change path to suit.

Take care

Marcus

Option Explicit
Const XLFILE_FOLDER = "C:\" 'Change to suit

Sub RecentXLFile()
Dim fs As Object
Dim objFolder As Object
Dim objFile As Object
Dim myFile As Object
Dim myDate As Date
myDate = DateValue("1/1/1900")

Set fs = CreateObject("Scripting.FileSystemObject")
Set objFolder = fs.GetFolder(XLFILE_FOLDER)

For Each objFile In objFolder.Files
If InStr(objFile.Name, ".xls") 0 Then 'xml can go here too.
If objFile.DateLastModified myDate Then
myDate = objFile.DateCreated
Set myFile = objFile
End If
End If
Next

If Not myFile Is Nothing Then
Workbooks.Open Filename:=myFile
End If

End Sub
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Excel file from VB and open MACRO no1jimmyman Excel Discussion (Misc queries) 0 February 14th 11 10:10 PM
2007 Macro to Open File, Delete Contents, Save New File Flintstone[_2_] Excel Discussion (Misc queries) 2 February 1st 10 11:25 PM
Open a file do a macro ( made) and open next succesive file SVTman74 Excel Programming 5 April 21st 06 10:14 PM
Macro to call a file that has a auto open macro in the file itself [email protected] Excel Programming 1 August 5th 05 06:39 AM
Automate open file, update links, run macro, close and save file Geoff[_7_] Excel Programming 2 August 26th 03 10:13 PM


All times are GMT +1. The time now is 10:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"