LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default macro to open latest file


Hi all,

I need my macro to open the most recent file in a folder. There is no
standard format to the file name except that they are always '.txt'.

I'm getting an error (Run-Time error '53'; File not found) on the
second run through the loop. I stole this code from someone;s posting,
and changed the extension from .xls to txt. I also changed mypath from
"thisworkbook.path & '\'" to what is below. Here's the loop:

Dim mypath As String
Dim checkfile As String
Dim checkdate As Date
Dim lastdate As Date
Dim lastfile As String


lastdate = #1/1/2001#
mypath = "F:\Users\Allison\FtCarsonMacro\Tenant files\"
checkfile = Dir(mypath & "*.txt")

Do While checkfile < ""
If checkfile < "." And checkfile < ".." Then
checkdate = FileDateTime(checkfile)
If checkdate lastdate Then
lastdate = checkdate
lastfile = checkfile
End If
End If
checkfile = Dir
Loop


The red text is where I'm getting the error. The following is the list
of files:

8.19.05 FTCarson APT.txt 8/19/2005
APT1.txt 8/11/2005
FT Tenant 8/25/2005

It runs the 8.19.05 FTCarson APT.txt first with no problems. Then it
tries to process the APT1.txt file and when it gets to checkdate =
FileDateTime(checkfile) it says it can't find the file. When I hold my
cursor over "checkfile" is does say "APT1.txt".

If it found the file to assign it to checkfile, why can't it find it
again? I'm so confused! Any help would be GREATLY appreciated!

Thanks in advance!


--
tsgol
------------------------------------------------------------------------
tsgol's Profile: http://www.excelforum.com/member.php...o&userid=26648
View this thread: http://www.excelforum.com/showthread...hreadid=399229

 
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 07:16 AM.

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

About Us

"It's about Microsoft Excel"