如果文件路径不存在则退出sub

时间:2014-02-17 23:18:54

标签: excel vba excel-vba save

嘿,我在Excel上相当新,我遇到了错误处理的问题。我希望我的宏运行,如果它找不到该文件夹​​,那么它应该向用户生成一条消息并退出子。任何帮助将不胜感激!

下面是我的宏保存文件的代码

    Worksheets("Input data").Visible = True
    folder = "\\Group_SHARED\Group Shared\Engineering\Controlled Folder\Number_Checkout\Archived\Archived"
    MyTime = Time
    Sheets("Input data").Select
    Range("G2").Value = MyTime
    strFileName = folder & "_" & Sheets("Input data").Range("C6").Value & "_" & Sheets("Get_ECN").Range("B6").Value & "_" & Sheets("Input data").Range("C3").Value & " " & Sheets("Input data").Range("C4").Value
    Worksheets("Input data").Visible = False
    ActiveWorkbook.SaveAs Filename:=strFileName

1 个答案:

答案 0 :(得分:0)

尝试:

    If dir(folder) = "" then
       Msgbox "no such directory"
       Exit sub
    end if

如:

Sub Test()
    folder = "C:\Users\Administrator\"
    If Dir(folder) = "" Then
       MsgBox "no such directory"
       Exit Sub
    Else
        MsgBox "Yup, It's There All Right."
    End If
End Sub