Baber62
Baber62

Reputation: 53

Excel macro to open file with partial filename

I have tried looking on various forums and cannot seem to find a solution that fits my needs.

I have a file "BABERs FORMULAS - 24 Jan 2017 - Rev 079 11-27.xlsm". I use a macro to save the changes to the file which adds the date, revision number and timestamp to the filename. The file is located at the following path "D:\FORMULAS".

The macro I have is as below but this then gives me a Run-time error '1004' and says to check that the spelling of the file name and verify the location is correct.

Dim fname as Variant
fname = Dir("D:\FORMULAS\BABERs FORMULAS*")

If fname <> "" then
    Workbooks.open (fname)
End If

Any ideas where I could be going wrong? Any assistance would be appreciated.

Upvotes: 2

Views: 21918

Answers (1)

Shai Rado
Shai Rado

Reputation: 33682

Try something like the code below:

Dim fname As Variant
Dim myPath As String

myPath = "D:\FORMULAS\"
fname = Dir(myPath & "BABERs FORMULAS*")

If fname <> "" Then
    Workbooks.Open (myPath & fname)
End If

Upvotes: 7

Related Questions