Saveas功能非常好。与单元格的名称
问题描述:
我想使以下代码工作以保存某种格式的文件名。我希望它保存在打开文件的文件夹中,文件会将其名称更改为新的月份名称。我已经完成了大部分工作,例如目录选择和文件名,然后保存,但是如果已经有一个同名的文件,或者如果有人选择否或取消它会给出错误。我尝试了各种方法来试图绕过它,但现在我不知所措。我有两个代码,他们都应该做同样的事情,只是变化。Saveas功能非常好。与单元格的名称
Sub saving1()
' Saves the file under a new name based on the new month date.
Dim NewFilename As String
Dim tempnm
Dim loc ' variable for file location
loc = Application.ThisWorkbook.Path 'loads the file location on the loc variable
MsgBox loc
' creates the file name for saving includes the current path.
NewFilename = loc + "\" + Range("NewFileName").Value & ".xlsm"
'tempmm = Application.GetSaveAsFilename initialfilename
ActiveWorkbook.SaveAs NewFilename, FileFormat:=52
'Application.DisplayAlert = False
'On Error Resume Next 'to omit error when cancel is pressed
' MsgBox "Not saved"
'ActiveWorkbook.Save
'If Err.Number <> 1004 Then 'optional, to confirmed that is not saved
' MsgBox "Not saved"
'End If
' On Error GoTo 0 'to return standard error operation
End Sub
Sub saving()
' Saves the file under a new name based on the new month date.
Dim NewFilename As String
Dim loc ' variable for file location
loc = Application.ThisWorkbook.Path 'loads the file location on the loc variable
' creates the file name for saving includes the current path.
NewFilename = loc + "\" + Range("NewFileName").Value & ".xlsm"
ActiveWorkbook.SaveAs NewFilename, FileFormat:=52
End Sub
我还添加了消息框来尝试看看它在测试过程中做的事情。我也尝试了Getsaveasfilename,以便给用户一个选择他/她自己的文件名和可能的文件夹的选项。文件位置将每年更改一次。
答
如果您正在查看覆盖现有文件,当已经有一个同名的文件尝试在下面。
NewFilename = loc + "\" + Range("NewFileName").Value & ".xlsm"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs NewFilename, FileFormat:=52
Application.DisplayAlerts = True
好的,谢谢。它确实有效。我之前尝试过,但我想我有他们(Application.displayalerts)在错误的地方。它的工作原理并没有更多的错误。我认为最好是没有选择文件名的选项。 – stever