0

My VBA is not that good so apologies in advance. I have some vba code but cant get it to work based on filename with Wildcards. It just ignores the condition There are over 120 excel files but I only want xlsx files that begin with specific text an example would be All_Regions.xlsx and I am trying to get a hit from a partial filename using wildcards.

I can get it to work from a static name but the issue is each week the file name will be different: All_Regions_01012018 to All_Regions_08012018 and so on each week.

Any assistance would be greatly appreciated.

Example below:

Sub Testing()
OPCO = "All_re*.xlsx"
Source = "C:\files\All_OPCO\"
StrFile = Dir(Source)
If OPCO = StrFile Then
    MsgBox ("Confirmed")
End If

End Sub

Community
  • 1
  • 1
Storm2508
  • 19
  • 1
  • 1
  • 6

1 Answers1

0

You need to include the file's search pattern within the Dir parameter and to test if it exists simply check that StrFile is not empty:

Try this:

Sub Testing()
    OPCO = "All_re*.xlsx"
    Source = "C:\files\All_OPCO\"
    StrFile = Dir(Source & OPCO)
    If StrFile<>"" Then
        MsgBox ("Confirmed")
    End If
End Sub
CLR
  • 11,284
  • 1
  • 11
  • 29