Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Opening file with date tags

Many thanks to Bob Phillips and Ivan for their help with picking up a file
with a date tag using code. (For some reason the listing has now disappeared)
I need to go a step further, I need the latest created file to be opened.
The code below shows how far I Have got:
Dim wb As Workbook
sPath = Application.DefaultFilePath & "\" & sFile
sfilename = "My File*.xls"
If Len(Dir(sPath & "\" & sfilename)) = 0 Then
MsgBox "No file"
Else
sfilename = sPath & "\" & Dir(sPath & "\" & sfilename)
Set wb = Workbooks.Open(sfilename)
End If
All the workbooks start with 'My File' and end with different dates & times,
and at the moment it only picks up the oldest file.
Again - any help greatly appreciated
John
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Opening file with date tags


Replace the line

Set wb = Workbooks.Open(sfilename)

with the following:

NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
Do while sfilename < ""
sfilename = sPath & "\" & dir()
if FileDateTime(sfilename) FileDate then
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
end if
loop
Set wb = Workbooks.Open(NewestFileName)


--
colofnature
------------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...o&userid=34356
View this thread: http://www.excelforum.com/showthread...hreadid=544793

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Opening file with date tags

Hi Colofnature - thanks for your help

I have now got:

Dim wb As Workbook
sPath = Application.DefaultFilePath & "\" & sFile
sfilename = "Front Page*.xls"
If Len(Dir(sPath & "\" & sfilename)) = 0 Then
MsgBox "No file"
Else
sfilename = sPath & "\" & Dir(sPath & "\" & sfilename)
'Set wb = Workbooks.Open(sfilename)
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
Do While sfilename < ""
sfilename = sPath & "\" & Dir()
If FileDateTime(sfilename) FileDate Then
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
End If
Loop
Set wb = Workbooks.Open(NewestFileName)
End If
End Sub

But for some reason it gets stuck on:

If FileDateTime(sfilename) FileDate Then

John

"colofnature" wrote:


Replace the line

Set wb = Workbooks.Open(sfilename)

with the following:

NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
Do while sfilename < ""
sfilename = sPath & "\" & dir()
if FileDateTime(sfilename) FileDate then
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
end if
loop
Set wb = Workbooks.Open(NewestFileName)


--
colofnature
------------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...o&userid=34356
View this thread: http://www.excelforum.com/showthread...hreadid=544793


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Opening file with date tags

What value does sFile have?

what do your filenames look like?

Assume the filename looks like Myfile20061015.xls

then you could do
Dim dtMax as Date, dt as Date
Dim s as String, s1 as String
Dim sDt as String, sPath as String
dtMax = 0
sPath = "C:\MyFiles\"
s = Dir(sPath & "Myfile*.xls")
do while s< ""
sDt = Mid(s,7,8)
dt = dateSerial(clng(left(sDt,1,4)),clng(mid(s,5,2)),cl ng(right(s,2)))
if dt maxDt then
s1 = s
maxDt = dt
end if
s = dir()
Loop
workbooks.open sPath & s

if your not making the determination by the date in the filename, then look
at the filedatetime function.

--
Regards,
Tom Ogilvy


"JohnUK" wrote:

Many thanks to Bob Phillips and Ivan for their help with picking up a file
with a date tag using code. (For some reason the listing has now disappeared)
I need to go a step further, I need the latest created file to be opened.
The code below shows how far I Have got:
Dim wb As Workbook
sPath = Application.DefaultFilePath & "\" & sFile
sfilename = "My File*.xls"
If Len(Dir(sPath & "\" & sfilename)) = 0 Then
MsgBox "No file"
Else
sfilename = sPath & "\" & Dir(sPath & "\" & sfilename)
Set wb = Workbooks.Open(sfilename)
End If
All the workbooks start with 'My File' and end with different dates & times,
and at the moment it only picks up the oldest file.
Again - any help greatly appreciated
John

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Opening file with date tags


Replace the line

Set wb = Workbooks.Open(sfilename)

with the following:

sfilename = sPath & "\" & Dir(sPath & "\" & sfilename)
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
Do While Len(sfilename) Len(sPath & "\")
If FileDateTime(sfilename) FileDate Then
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
End If
sfilename = sPath & "\" & Dir()
Loop
Set wb = Workbooks.Open(NewestFileName)

I haven't tested it, but it ought to work... :)
Col


--
colofnature
------------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...o&userid=34356
View this thread: http://www.excelforum.com/showthread...hreadid=544793



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Opening file with date tags

Great it works

Many thanks colofnature

Also thanks to Tom - I will try yours later

Must leave now.........

"" wrote:


Replace the line

Set wb = Workbooks.Open(sfilename)

with the following:

sfilename = sPath & "\" & Dir(sPath & "\" & sfilename)
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
Do While Len(sfilename) Len(sPath & "\")
If FileDateTime(sfilename) FileDate Then
NewestFileName = sfilename
FileDate = FileDateTime(sfilename)
End If
sfilename = sPath & "\" & Dir()
Loop
Set wb = Workbooks.Open(NewestFileName)

I haven't tested it, but it ought to work... :)
Col


--
colofnature
------------------------------------------------------------------------
colofnature's Profile: http://www.excelforum.com/member.php...o&userid=34356
View this thread: http://www.excelforum.com/showthread...hreadid=544793


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
Opening a file with date extension JohnUK Excel Programming 13 May 22nd 06 12:30 PM
Opening file saved as current date Yepp12[_2_] Excel Programming 3 December 13th 05 02:59 PM
switched date format opening dbf file [email protected] Excel Programming 1 July 16th 04 01:13 PM
Date format when opening a .CSV file programmatically jason Excel Programming 3 November 10th 03 09:09 AM
Opening CSV file causes date differences Tom Ogilvy Excel Programming 0 August 6th 03 12:26 AM


All times are GMT +1. The time now is 02:04 PM.

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"