Wildcard file open

Zabman

Board Regular
Joined
Apr 7, 2010
Messages
77
Hi,

I have some code that determines if a directory has any files present as follows:
Code:
Function FileChk() As Boolean
' Determine if there are files to convert
    strFile = Dir("c:\TNT\")
    If strFile = "" Then
        FileChk = False
        MsgBox "No New Orders Found"
    ElseIf strFile <> "" Then
        FileChk = True
    End If
End Function

This works fine, but I am struggling to open files with a wildcard search:

Code:
Workbooks.Open Filename:="C:\TNT\*.xlsx"

There can be anywhere from o to 50 files in this folder, but the code is returning a 'file cant be found (runtime 1004 I think) error.

Can anyone help?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I had some code that counted files of certain extension in a directory. I changed it around a little to open files. I test it twice and got it to work without any errors opening 6 empty 'xlsx' files.
Code:
Sub OpenFiles()

    Dim Folder As Object
    Dim file As Object
    Dim fso As Object
    
    Dim numFiles As Integer
    Dim folderPath As String, fileExt As String
        
    folderPath = "D:\Test\"
    fileExt = "xlsx"
       
   Set fso = CreateObject("Scripting.FileSystemObject")
   
    If folderPath <> "" Then
        Set Folder = fso.GetFolder(folderPath)
        
        For Each file In Folder.Files
            If Right(file, Len(fileExt)) = fileExt Then
                Workbooks.Open Filename:=file
            End If
        Next file
    End If

End Sub

EDIT: It would error out if a file was already open as it tried to open the hidden file '~$FileName.xlsx'
 
Last edited:
Upvote 0
To prevent it from opening hidden files which are prefixed with '~$' replace
Code:
If Right(file, Len(fileExt)) = Then
in the For Each file Loop with
Code:
If Right(file, Len(fileExt)) = fileExt And Mid(file, Len(folderPath) + 1, 2) <> "~$" Then
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top