确定文件是否存在

我想打开工作簿到存档列表中的variables。

如果我没有存档中的文件,我希望它显示一个消息框,但它不起作用。

strVariable = Left(PictureNo, 4) d = "Teknik Resim Arsiv Listesi_" & strVariable & ".xls" Dim Ret Ret = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & d) If Ret = False Then MsgBox "Not Found" End If 

尝试打开文件之前检查文件是否存在:

 strVariable = Left(PictureNo, 4) d = "Teknik Resim Arsiv Listesi_" & strVariable & ".xls" If Dir(ThisWorkbook.Path & Application.PathSeparator & d) = "" Then MsgBox "Not Found" Else Dim wb As Workbook Set wb = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & d) End If