Search for flename in directory

Ahmed

New Member
Joined
May 16, 2005
Messages
17
I am using a macro to automate an openfile from files ina specific directory. The file contains the date in the yyyymmdd format which isnt a problem. im iteratioing through the file searching for a file containing any of the last nine days as follows:

Sub test()

Dim i As Integer
Dim xlFileName As String
i = 0

ChDrive "E" ' set default drive
ChDir "E:\test"

While i < 9

xlFileName = "anc" & Format$(Now - i, "yyyymmdd") & "Sales.xls"
If (xlFileName = True) Then ***this part is where I am having difficulties, dont know how to test its existance
msgbox 'just to see if it work or not

End If
i = i + 1
Wend

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi A,

How about?
Code:
Sub test()
    Dim i As Integer
    Dim xlFileName As String
    i = 0
    
    ChDrive "C" ' set default drive
    ChDir "C:\test"
    
    While i < 9
        xlFileName = "anc" & Format$(Now - i, "yyyymmdd") & "Sales.xls"
        If Len(Dir(xlFileName)) > 0 Then
            MsgBox xlFileName 'just to see if it work or not
        End If
        i = i + 1
    Wend

End Sub
HTH
 
Upvote 0
The length wont work as all the files are of similar format, i tried it and unfortunately it doesnt accomplish what i need it ot do
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top