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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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