简体   繁体   English

Saveas函数excel。 来自单元格的名称

[英]Saveas function excel. with name from cell

I am trying to make the following code work for saving a file name in a certain format. 我正在尝试使以下代码用于以某种格式保存文件名。 I would like it to save in the folder the file was opened up in. the file would change it's name to a new month name. 我希望将其保存在打开该文件的文件夹中。该文件会将其名称更改为新的月份名称。 I have got most of it working, such as directory selection and filename and for it to save, however, if there is already a file with the same name or if someone selects no or cancel it gives an error. 我已经完成了大部分工作,例如目录选择和文件名,并保存了该文件,但是,如果已经有一个同名文件,或者有人选择了否或取消该文件,则会出现错误。 I have tried various ways of trying to get around it but now I'm at a loss. 我尝试了各种方法来解决它,但是现在我茫然了。 I have 2 codes they both are supposed to do the same thing, just variations. 我有2个代码,它们都应该做相同的事情,只是变化而已。

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

I also added message boxes to try see what it is doing during testing. 我还添加了消息框,以尝试查看其在测试过程中的作用。 I have also tried the Getsaveasfilename in order to give the user an option to choose his/her own filename and possibly folder. 我还尝试了Getsaveasfilename,以便为用户提供一个选择自己的文件名和文件夹的选项。 The file location will change once a year. 文件位置每年更改一次。

If Your are looking at overwriting existing file, when there's already a file with same name try below. 如果您正在考虑覆盖现有文件,请在已有同名文件的情况下尝试以下操作。

NewFilename = loc + "\" + Range("NewFileName").Value & ".xlsm"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs NewFilename, FileFormat:=52
Application.DisplayAlerts = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM