View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel[_522_] joel[_522_] is offline
external usenet poster
 
Posts: 1
Default Count File Names


See if this code works. I assume that 2010-01 is year and month.


Sub GetLatestFile()


Folder = "c:\temp\"

LatestDate = 0
LatestVersion = 0
LatestFName = ""

'make sure folder name has last backslash
If Right(Folder, 1) < "\" Then
Folder = Folder & "\"
End If

CountYear = 0
FName = Dir(Folder & "*.xls")
Do While FName < ""
'split file name into two piece around the space
NewVersionArray = Split(FName, " ")
'spit the date portion of the file name into year and month
NewDateArray = Split(Trim(NewVersionArray(0)), "-")
'convert the date to a number (serial date), use the first day of
the month
NewDate = DateSerial(NewDateArray(0), NewDateArray(1), 1)

'count if file is this year
If Year(NewDate) = 2010 Then
CountYear = CountYear + 1
End If

'remove the V from verion so version is a number
NewVersion = Val(Mid(Trim(NewVersionArray(1)), 2))

If NewDate LatestDate And _
NewVersion LatestVersion Then

LatestDate = NewDate
LatestVersion = NewVersion
LatestFName = FName
End If

FName = Dir()
Loop

'add folder name to latest file name
LatestFName = Folder & LatestFName


End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=169466

Microsoft Office Help