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!

8
  • you have to use Dir function to open file through wildcards. techonthenet.com/excel/formulas/dir.php Commented Feb 13, 2016 at 19:06
  • 1
    Try to change If Fl.Name = F_Name Then to If Fl.Name Like F_Name Then Commented Feb 13, 2016 at 19:07
  • This answer will gonna help you go through it stackoverflow.com/questions/19527415/… Commented Feb 13, 2016 at 19:10
  • but after search how can I open that particular file Commented Feb 13, 2016 at 19:11
  • 1
    Change F_Name to Fl.Name in this line Workbooks.Open .... Commented Feb 13, 2016 at 19:38

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

May I direct open the file with out searching ?
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.