1
Sub Main()
Dim FSO As New FileSystemObject
Dim Fl As File
Dim Folder As Folder
Dim F_Name, F_Path As String
F_Path = ThisWorkbook.Path & "\"
Set Folder = FSO.GetFolder(F_Path)
F_Name = "CI*.*"
For Each Fl In Folder.Files
    If Fl.Name = F_Name Then
        GoTo Report
    End If
Next

Report:
Workbooks.Open Filename:=F_Path & F_Name

I would like to open a excel file with same location but I know only part of file name so please assist how can I open the file name. Thanks!

Community
  • 1
  • 1
Developer_World
  • 63
  • 2
  • 14

2 Answers2

1

Try:

Sub Main()
Dim FSO As New FileSystemObject
Dim Fl As File
Dim Folder As Folder
Dim F_Name, F_Path As String
F_Path = ThisWorkbook.Path & "\"
Set Folder = FSO.GetFolder(F_Path)
F_Name = "CI*.*"
For Each Fl In Folder.Files
    If Fl.Name Like F_Name Then
        GoTo Report
    End If
Next

msgbox "File not found"
exit sub

Report:
Workbooks.Open Filename:=F_Path & Fl.Name
End Sub

Edit:

To search using Dir :

Sub TestDir()

    Dim F_Path As String, F_Name As String, f As String

    F_Path = ThisWorkbook.Path & "\"
    F_Name = "CI*.*"

    f = Dir(F_Path & F_Name)

    If f = "" Then
     MsgBox "File not found"
    Else
     Workbooks.Open FileName:=F_Path & f
    End If

End Sub
Fadi
  • 3,302
  • 3
  • 18
  • 41
  • May I direct open the file with out searching ? – Developer_World Feb 14 '16 at 01:58
  • @VBA_Coder, if you don't know the full name of the file I don't think you can open it without searching , but we can use `Dir` to search without looping (faster), see edit. – Fadi Feb 14 '16 at 06:35
0
Sub Main()
Dim FSO As New FileSystemObject
Dim Fl As File
Dim Folder As Folder
Dim F_Name, F_Path As String
F_Path = ThisWorkbook.Path & "\"
Set Folder = FSO.GetFolder(F_Path)
F_Name = "CI*.*"
For Each Fl In Folder.Files
If Fl.Name Like F_Name Then
    GoTo Report
End If
Next

Report:
Workbooks.Open Filename:=F_Path & Fl.Name
Developer_World
  • 63
  • 2
  • 14