3

I am trying to get Excel to open any file in the a given folder (ThisWorkbook.Path\Peach\Apple) that has .xlsm extension (there is always only 1 file). Is it possible to open it with wildcard character? I do not know the name of the file, just the extension.

If not, is there a way to do it?

4 Answers 4

5

Just ask the file system for the first matching file:

Dim path As String: path = ThisWorkbook.path & "\Peach\Apple\"

FindFirstFile = Dir$(path & "*.xlsm")

If (FindFirstFile <> "") Then 
   Workbooks.Open path & FindFirstFile
Else
   '// not found
End If

(This will not search sub-directories)

Sign up to request clarification or add additional context in comments.

4 Comments

How does it select the first file? Based on filename?
In an undefined fashion
Depending on folder sort options. Should be alphabetic.
Yea that's often the observed case but its explicitly documented that the order is not guaranteed so should never be relied upon.
2

You mentioned that it would be nice addition to open last modified file or file with shortest name, so let's start - there's a code example how you can grab all three files (first finded, last modified, with shortest name). You can modify this as you wish (add some parameters, add error handling, return only specified, etc).

Sub Test()
   'declarations
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim path As String

    Dim first_finded As Object
    Dim recently_modified As Object
    Dim shortest_name As Object
    Dim recently As Date
    Dim shortest As Long
    Dim firstFinded As Boolean

    'setting default recently date(24 hours from now) and path
    recently = DateAdd("h", -24, Now)
    path = ThisWorkbook.path & "\Peach\Apple\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(path)


    'iteration over folder
    For Each file In folder.Files
        If file.Name Like "*.xlsm" Then

            'grab first finded .xlsm
            If Not firstFinded Then
                firstFinded = Not firstFinded
                Set first_finded = file
            End If

            'grab lastmodified .xlsm
            If file.DateLastModified > recently Then
                recently = file.DateLastModified
                Set recently_modified = file
            End If

            'grab short named .xlsm
            If shortest = 0 Or shortest > Len(file.Name) Then
                shortest = Len(file.Name)
                Set shortest_name = file
            End If
        End If
    Next

    'debug-print names
    Debug.Print first_finded.Name
    Debug.Print recently_modified.Name
    Debug.Print shortest_name.Name

    'so now you can uncomment this and open what you want
    'Call Workbooks.Open(path & recently_modified.Name)

End Sub

Comments

1

Try the code below, it will open your "*.xlsm" file, in the path you've requested.

Sub OpenXLSMWildcardfile()

Dim Path As String

Path = ThisWorkbook.Path & "\Peach\Apple\"
Workbooks.Open (Path & "*.xlsm")

End Sub

8 Comments

What if there are files e.g. in ThisWorkbook.Path\Peach\Apple\Orange that also have .xlsm extension? Will it try to open them as well?
@user628797 no, you asked in your post for a specific folder, do you want to search all sub-folders as well? What happens if it finds 2 ".xlsm" files ? should it open the first one it finds randomly ?
The answer to the first question is no, this works exactly as I wanted to far, thank you. The answer to the second question is: what are the options?
@user628797 there are a few, do you know what you want ? (or need) ?
The one with the shortest filename.
|
1

PFB for the code required for opening the macro file with extension(.xlsm).

Sub OpeningFile()

'Declaring variables
Dim FileName, FolderPath As String

'Initializing folder path
FolderPath = ThisWorkbook.Path & "\Peach\Apple\"

'Finding the file name using wildcard
FileName = Dir(FolderPath & "*.xlsm")

'Looping through the workbook which are saved as macro enabled workbooks
While FileName <> ""
    Workbooks.Open FolderPath & FileName
    FileName = Dir()
Wend

End Sub

1 Comment

Your first variable "FileName" is a variant. You can't declare variables in VBA this way. You need to add "As String" to every item. cpearson.com/excel/declaringvariables.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.