Excel VBA文件保存

时间:2017-05-23 18:24:05

标签: excel excel-vba vba

我正在尝试在Excel模板上设置自动保存功能,以便原始模板不会被写入。

Dim NameFile As String

'Gets the users username
UserName = Environ$("UserName")

'User is to input data type 
datat= Application.InputBox("Enter a Data Type", "Data Type")

    With Worksheets("Home")



    'Sets up auto filename with YearMonthDay - Username - Filename(From a specific Cell) - Data Type
    NameFile = Format(Date, "yyyymmd") & " - " & UserName & " - " & Range("A1") & " - " & datat & ".xlsm"
    End With
    'Sets up save location
    NameFile = Application.GetSaveAsFilename(InitialFileName:=Environ("USERPROFILE") & "\Desktop\" & NameFile, Filefilter:=" Excel (*.xlsm), *.xlsm")

    If NameFile = False Then
    'Tell user with a caution that the file has not been saved

    MsgBox "File not saved", vbCritical, "Caution"

    Exit Sub

    Else
    ThisWorkbook.SaveAs Filename:=NameFile

    MsgBox "File Saved"
    End If

当我不保存文件并单击取消时,我会收到消息框,告诉我该文件尚未保存...这就是我想要的。

但是当我使用给定名称保存文件时,我收到运行时错误' 13',输入不匹配

我做错了什么?

1 个答案:

答案 0 :(得分:1)

Dim NameFile Variant

这样它可以包含 Boolean String