VBA文件search

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 

我想打开一个具有相同位置的excel文件,但我只知道文件名的一部分,所以请协助如何打开文件名。 谢谢!

尝试:

 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 

编辑:

使用Dirsearch:

 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 
 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