我试图在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',类型不匹配
我做错了什么?
发布于 2017-05-23 18:56:04
您应该将Dim NameFile作为变体。
这样,它就可以容纳布尔值或字符串。
https://stackoverflow.com/questions/44142358
复制相似问题