excelvbafilenamespartial

Opening a file with partial name


I am trying to open a file with partial name.

The current name is "GDE Portfolio Characteristics 12.31.2021".

The idea is to open it, no matter the date (last 10 characters). I should only have one file in the folder with such a partial name.

Workbooks.Open Filename:=ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx"

It does not find the file. It works if I use the entire name of the file.


Solution

  • There is no way to use a wildcard in the Open-statement. However, you can use the Dir-command to get the real file name as it allows wildcards:

    Dim fileName As String
    fileName = Dir(ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx")
    If fileName <> "" Then
        Workbooks.Open Filename:=ThisWorkbook.Path & "\" & fileName
    End If