CJ See
CJ See

Reputation: 11

Open Excel Files that Contains a Specific Date

In my folder, I have these files:

File1_010119, File2_010119, File3_030119, File4_050119, File5_050119

The current code I have opens all the excel files in the given directory. Is there a way to only open files that contains a specific date for example (010119)?

directory = "C:\Users\Student\Desktop\BI\Assignment\Datasets\"
fileName = Dir(directory & "*.xl??")
currentFile = "Main Excel File.xlsm"

Do While fileName <> ""
    Workbooks.Open (directory & fileName)

Upvotes: 0

Views: 78

Answers (1)

TourEiffel
TourEiffel

Reputation: 4414

Maybe try this :

directory = "C:\Users\Student\Desktop\BI\Assignment\Datasets\"
fileName = Dir(directory & "*.xl??")
currentFile = "Main Excel File.xlsm"

Do While fileName <> ""

If InStr(fileName, "010119") > 0 Then Workbooks.Open (directory & fileName)

or pᴇʜ's solution is working to :

directory = "C:\Users\Student\Desktop\BI\Assignment\Datasets\"
fileName = Dir(directory & "*010119.xl??")
currentFile = "Main Excel File.xlsm"

Do While fileName <> ""

Workbooks.Open (directory & fileName)

This way will browse only file with 010119.xl in. And the way I propose you browse all .xl file then check the fileName

If you just want to browse file with 010119.xl I would recommend you tu use the second way. And if you want to browse all file the first.

Upvotes: 1

Related Questions