If DIR finds more than one?

E

Ed

I'm telling my code to find a folder using
FolderName = Dir$("C:\Documents and Settings\emillis\TIR *", vbDirectory)
If FolderName = "" Then
MsgBox "Not found"
Exit Sub
End If

Can I add something in case DIR found more than one match that will let me
choose which directory?

Ed
 
P

Peter Hewett

Hi Ed

The key question here is what are you going to do with the list once you have it? How are
you going to select the appropriate folder?

HTH + Cheers - Peter
 
E

Ed

Hi, Peter. I could handle it either by showing a dialog box and manually
choosing, or by setting up code to pick which one. The "*" is a date - I
could pull the dates off each name and compare them; I would choose the
latest date. I just don't know enough about using DIR (yet! but I'm
learning) to figure this one out.

Ed
 
P

Peter Hewett

Hi Ed

Here's a version that will choose the folder with the latest date (if the is a folder with
a date). The code does not care about the date format used in the folder name, but you
will need to reconstitute it at the end. Use a Format$ statement to correctly format the
date so that you can rebuild the folder name:

Public Sub SelectFolderWithLatestDate()
Dim dateNull As Date
Dim dateFile As Date
Dim dateLatest As Date
Dim strFolder As String

' Get the first matching folder (if any)
strFolder = Dir$("c:\documents and settings\emillis\tir *", vbDirectory)

' Iterate all the folders looking for the one with the latest date
Do Until LenB(strFolder) = 0

' We want the date part of the folder name
dateFile = CDate(Mid$(strFolder, 5))

' We want the latest date
If dateFile > dateLatest Then
dateLatest = dateFile
End If

' Next folder (if any)
strFolder = Dir$
Loop


If dateLatest <> dateNull Then
MsgBox "Latest folder: " & Format$(dateLatest, "dd mmm yyyy")
Else
MsgBox "No matching folders were found"
End If
End Sub

HTH + Cheers - Peter
 
E

Ed

Peter: Due to computer problems, I didn't have a chance to check out the
code until this morning. It works like a champ! Thank you so much!

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top