确定文件是否存在

时间:2017-08-21 08:24:14

标签: excel vba

我想在存档列表中打开工作簿到变量。

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

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

1 个答案:

答案 0 :(得分:1)

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

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