首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA文件保存

Excel VBA文件保存
EN

Stack Overflow用户
提问于 2017-05-23 18:24:05
回答 1查看 142关注 0票数 0

我试图在excel模板上设置一个自动保存函数,这样原始模板就不会被编写完。

代码语言:javascript
复制
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',类型不匹配

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-23 18:56:04

您应该将Dim NameFile作为变体。

这样,它就可以容纳布尔值或字符串。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44142358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档