Hi Som. DocAElstein has already provided a superb response to your request which includes alternate solutions. For the sake of completeness and because of my disdain for the use of the Dir function, I would like to offer the following which uses the file system object to search the directory and the Instr function to select the needed files. HTH. Dave
Private Sub Test() Dim objFSO As Object, PathFolder As String Dim objFolder As Object, objFile As Object '****adjust YourFolderName to suit path PathFolder = "C:\YourFolderName\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(PathFolder) 'loop all files in folder For Each objFile In objFolder.Files 'access only XL files If objFile.Name Like "*" & ".xls" & "*" Then 'open only file names with "cp4" or "gt4" and not "ghl" If InStr(objFile.Name, "cp4") Or InStr(objFile.Name, "gt4") And _ Not InStr(objFile.Name, "ghl") Then Workbooks.Open Filename:=objFile 'do stuff Workbooks(objFile.Name).Close savechanges:=True End If End If Next objFile Set objFolder = Nothing Set objFSO = Nothing End Sub




Reply With Quote