Sub DefineFile()
Dim myFiles As Variant
Dim docname As String
Dim myCountOfFiles As Variant
'this inputbox could accept any file name specified without the extension, however it can be useful when accessing files_
'stored with ids or reference numbers.
docname = InputBox("Type in the file name you want to process")
If docname = "" Then
MsgBox "No file name entered", vbInformation, "Cancelled"
Exit Sub
End If
Application.DisplayAlerts = False
'this part will search the folder specified and all its subfolder for the file name entered
'it will only check for the file with an .xls extension, change as required...you can use wildcards
myCountOfFiles = Get_File_Names( _
MyPath:="C:UsersadminDesktop", _
Subfolders:=True, _
ExtStr:=docname & "*.*", _
myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "File name " & docname & " has not been entered correctly or does not exist."
Exit Sub
End If
'this part will call the Get_Data module and open the file specified if found
OpenFile myReturnedFiles:=myFiles
End Sub
Sub OpenFile(myReturnedFiles As Variant)
Dim mybook As Workbook
Dim I As Long
'turn off ScreenUpdating and EnableEvents
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0
Next I
End Sub
__________________________________________
'DO NOT MODIFY THESE CODE EXCEPT EXPERIENCED
Option Explicit
Private myFiles() As String
Private Fnum As Long
Function Get_File_Names(MyPath As String, Subfolders As Boolean, _
ExtStr As String, myReturnedFiles As Variant) As Long
Dim Fso_Obj As Object, RootFolder As Object
Dim SubFolderInRoot As Object, file As Object
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "" Then
MyPath = MyPath & ""
End If
'Create FileSystemObject object
Set Fso_Obj = CreateObject("Scripting.FileSystemObject")
Erase myFiles()
Fnum = 0
'Test if the folder exist and set RootFolder
If Fso_Obj.FolderExists(MyPath) = False Then
Exit Function
End If
Set RootFolder = Fso_Obj.GetFolder(MyPath)
'Fill the array(myFiles)with the list of Excel files in the folder(s)
'Loop through the files in the RootFolder
For Each file In RootFolder.Files
If LCase(file.Name) Like LCase(ExtStr) Then
Fnum = Fnum + 1
ReDim Preserve myFiles(1 To Fnum)
myFiles(Fnum) = MyPath & file.Name
End If
Next file
'Loop through the files in the Sub Folders if SubFolders = True
If Subfolders Then
Call ListFilesInSubfolders(OfFolder:=RootFolder, FileExt:=ExtStr)
End If
myReturnedFiles = myFiles
Get_File_Names = Fnum
End Function
Sub ListFilesInSubfolders(OfFolder As Object, FileExt As String)
Dim SubFolder As Object
Dim fileInSubfolder As Object
For Each SubFolder In OfFolder.Subfolders
ListFilesInSubfolders OfFolder:=SubFolder, FileExt:=FileExt
For Each fileInSubfolder In SubFolder.Files
If LCase(fileInSubfolder.Name) Like LCase(FileExt) Then
Fnum = Fnum + 1
ReDim Preserve myFiles(1 To Fnum)
myFiles(Fnum) = SubFolder & "" & fileInSubfolder.Name
End If
Next fileInSubfolder
Next SubFolder
End Sub |