Excel VBA - 为什么我的“另存为”不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22305146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:16:10  来源:igfitidea点击:

Excel VBA - Why is my "Save As" not working?

excelvbaexcel-vba

提问by Petay87

I have a VBA script in place so that if a cell is blank then Excel will prompt the file to be saved.

我有一个 VBA 脚本,因此如果单元格为空白,则 Excel 将提示保存文件。

This is ensure that the template is not altered. However, when the user clicks save in the "Save As" dialogue box, the file does not save.

这是确保模板不被更改。但是,当用户在“另存为”对话框中单击保存时,文件不会保存。

This is the code I am using:

这是我正在使用的代码:

If Worksheets("Input").Range("E2").Value = "" Then
    Application.EnableEvents = False
    Application.GetSaveAsFilename InitialFileName:="\ac35542\Problem Management\Action Plans\ChangeMe.xlsm", FileFilter:="Excel Macro-Enabled Workbook (*.xlsm),*.xlsm"
    Application.EnableEvents = True
    MsgBox "Please ensure fill in the Problem Reference Number, Problem Title, and Select a Contract", vbExclamation, "PR Reference & Title"
    Worksheets("Input").Select
    Range("E2").Select
End If

Why is the file not saving?

为什么文件没有保存?

回答by Dmitry Pavliv

As follow up from MSDN

MSDN跟进

Application.GetSaveAsFilename displays the standard Save As dialog box and gets a file name from the user without actually saving any files..

Application.GetSaveAsFilename 显示标准的另存为对话框并从用户那里获取文件名,而无需实际保存任何文件。.

use this one instead:

改用这个:

Dim fileSaveName
If Worksheets("Input").Range("E2").Value = "" Then
    Application.EnableEvents = False
    fileSaveName = Application.GetSaveAsFilename(InitialFileName:="\ac35542\Problem Management\Action Plans\ChangeMe.xlsm", FileFilter:="Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
    Application.EnableEvents = True

    If fileSaveName <> "False" Then
        Application.DisplayAlerts = False
        ThisWorkbook.SaveAs (fileSaveName)
        Application.DisplayAlerts = True
    End If

    MsgBox "Please ensure fill in the Problem Reference Number, Problem Title, and Select a Contract", vbExclamation, "PR Reference & Title"
    Worksheets("Input").Select
    Range("E2").Select
End If

回答by Armani

I think Dmitry Pavliv's method is fine, but I think the "InitialFileName:="\ac35542\Problem Management\Action Plans\ChangeMe.xlsm" part makes it a little bit less dynamic.

我认为 Dmitry Pavliv 的方法很好,但我认为“InitialFileName:="\ac35542\Problem Management\Action Plans\ChangeMe.xlsm”部分使它的动态性降低了一点。

For me, the below code worked perfectly:

对我来说,下面的代码工作得很好:

ExportPath = Application.GetSaveAsFilename(FILEFILTER:="Excel Files (*.xlsx), *.xlsx", Title:="")
'Basically, user will specify the path and give it a name and click on Save. It won't get saved until the next line though.
ActiveWorkbook.SaveAs (ExportPath)