Option Explicit
Private Sub btnBrowse_Click()
tbPath.Text = Replace(BrowseForFolder, "\\", "\")
End Sub
Private Sub btnScan_Click()
Me.ProgressLabel.Caption = ""
If Len(Me.tbPath.Text) And Len(Me.TBSheetName.Text) Then
Dim FSO As Object, FSOFolder As Object, FSOItem As Object, SheetNames As String, Counter As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSO.Getfolder(tbPath.Text)
For Each FSOItem In FSOFolder.Files
DoEvents
Counter = Counter + 1
Me.ProgressLabel.Caption = "Processing " & Counter & " of " & FSOFolder.Files.Count & " files"
If FSOItem.name Like "*.xls*" Then
SheetNames = GetSheetNames(FSOItem.Path)
If InStr(SheetNames, TBSheetName.Text) Then
Me.ListBox1.AddItem FSOItem.Path
End If
End If
Next
End If
ProgressLabel.Caption = Me.ListBox1.ListCount & " files found"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Application.Workbooks.Open ListBox1.Text
End Sub
Function BrowseForFolder(Optional ByVal Prompt As String = "Please select folder") As String
BrowseForFolder = CreateObject("Shell.Application").BrowseForFolder(0, Prompt, 1, "").self.Path & "\"
End Function
Function GetSheetNames(ByVal FilePath As String) As String
Dim Conn As Object, RecSet As Object
Set Conn = CreateObject("ADODB.Connection")
Set RecSet = CreateObject("ADODB.Recordset")
On Error GoTo ErrHandler
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & ";Extended Properties=Excel 12.0 Macro;"
Set RecSet = Conn.OpenSchema(20) ' 20 = adSchemaTables
While Not RecSet.EOF
GetSheetNames = GetSheetNames & IIf(Trim(GetSheetNames) = "", "", ",") & RecSet("TABLE_NAME")
RecSet.MoveNext
Wend
RecSet.Close
Conn.Close
ErrHandler:
End Function