Cindy Ellis
MrExcel MVP
- Joined
- Jun 9, 2006
- Messages
- 1,802
Using Excel 2007:
I am trying to use dir() in a While...wend to get a specific list of files to process, but I want the list to only include those with a .xls extension, not those ending in .xlsx. It seems that the file filter ".xls" doesn't exclude those files with a ".xlsx" extension.
Sample code demonstrating the problem (will show the problem if there is at least one cycle that meets a*.xls and one that meets a*.xlsx).
Is there a workaround to this at the dir() level? Or do I need to go ahead and open the file and test its contents?
Thanks for any help,
Cindy
I am trying to use dir() in a While...wend to get a specific list of files to process, but I want the list to only include those with a .xls extension, not those ending in .xlsx. It seems that the file filter ".xls" doesn't exclude those files with a ".xlsx" extension.
Sample code demonstrating the problem (will show the problem if there is at least one cycle that meets a*.xls and one that meets a*.xlsx).
Is there a workaround to this at the dir() level? Or do I need to go ahead and open the file and test its contents?
Code:
Sub TestDir()
On Error GoTo Nofile 'trap cancel, X-out of the dialog, etc.
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.InitialFileName = "a*.xls"
.Show
GetDir = .SelectedItems(1)
End With
On Error GoTo 0 'reset error handler
If GetDir <> "False" Then
Path = CurDir & "\"
Else
MsgBox "Directory not selected"
Exit Sub
End If
df = Dir(Path & "a*.xls") 'should get the list of a*.xls files in the directory, but gets a*.xlsx too
While df <> ""
MsgBox (df)
df = Dir()
Wend
GoTo OK 'skip the error message
Nofile:
MsgBox ("No file selected. Macro will exit.")
Exit Sub
OK:
End Sub
Cindy