View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Incidental Incidental is offline
external usenet poster
 
Posts: 226
Default Open files from folder by matching name

Hi There

One method you could use is the File System Object which is handy in
this sort of situation. It will check each file in the given folder
to see if it is an Excel file if so you can use the InStr function to
look for a string within a string to check the name of the file and if
you get a match you can then open the file or do what ever else you
need.

Option Explicit

Sub OpenFilesUsingFSO()

Dim intRow As Integer
Dim intLastRow As Integer
Dim objFSO As Object
Dim objFile As Object
Dim objFolder As Object
Dim strSearchFor As String

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getfolder("C:\Test")

With workbooks("Data.xls")
intLastRow = .Sheets("Sheet1").Cells _
(Rows.Count, "A").End(xlUp).Row

For intRow = 1 To intLastRow

strSearchFor = .Sheets(1).Cells(intRow, 1).Value

For Each objFile In objFolder.Files
If Right(objFile.Name, 4) = ".xls" Then
If InStr(1, objFile.Name, strSearchFor, vbTextCompare) <
0 Then
Workbooks.Open (objFile)
End If
End If
Next objFile
Next intRow
End With

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing

End Sub

I hope this helps you out

Steve