VBA - 检查有效文件路径是否未按预期工作

问题描述 投票:0回答:1

我正在为工作编写一个宏,最后,试图保存。它将可供组访问,因此保存文件的位置不会是静态的。作为解决方法,我要求用户指定文件的保存位置,并在文件路径无效时抛出错误。无论我尝试什么,它总是说我的文件路径无效。你能看出我做错了什么吗?

Dim CurrDate As String
Dim SaveLoc As String
Dim SaveBook As String
    
    CurrDate = Format(Date, "MMDDYY")

        SaveLoc = InputBox("Please specify the folder with file path where you would like to save your file:")
        SaveBook = SaveLoc + "\" + "MyFile - " & CurrDate
    
        If Dir(SaveBook) <> "" Then
            ActiveWorkbook.Save
            ActiveWorkbook.SaveAs Filename:=SaveBook
        Else
            MsgBox "File path does not exist. Please enter a valid file path:"

        End If
excel vba error-handling path
1个回答
0
投票

有一个比InputBox更好的替代品。

Application.FileDialog(msoFileDialogSaveAs)

链接到 Microsoft Learn of Excel.Application.FileDialog

With Application.FileDialog(msoFileDialogSaveAs)
    .Title = "xxx"
    .AllowMultiSelect = False
    .InitialFileName = "xxx.csv"
    result = .Show
    If (result <> 0) Then
        GetSaveFilename = .SelectedItems(1)
        ' create file
    End If
End With
© www.soinside.com 2019 - 2024. All rights reserved.