I have an issue with opening a file using pattern matching. The purpose is opening a file which has a patern like this : extraction _ 20180630 _ Data - Updated.Xlsx
. This part of the file name : 201806
is set by the users via an input box function using variables as text. The goal is to open this file knowing that the dates can change depending on the target chosen by the users (year and month) .
Here is my code :
Sub OpenFile
Dim Directory As Text
Dim File As Text
Dim MainPath As Text
Dim Y As Text
Dim M As Text
Directory = "C:\Desktop\Folder\"
Y = InputBox ("Please Choose your target Year such as 2017")
M = InputBox ("Please Choose your target Month such as 06 for June")
' File name format = "extraction _ 20180630 _ Data - Updated _V5.2.xlslx"
File = "extraction _" & Y & M
MainPath = Directory & File
Filename = Dir (MainPath & "*_ Data - Updated _*")
On Error Resume Next
' after several Checking, the MainPath is Correct. the issue is whithin the second part
Workbooks.Open MainPath & Filename
If Err.Number = 1004
MsgBox " The file was not found"
Err.Clear
Application.DisplayAlerts = False
Application.Quit
End If
End Sub
As You can imagine, this code generates an
error 1004
and I checked the main path which is correct. The issue is with the second part of Filename as Follows *_ Data - Updated _*
)
How can I solve this?