vba - 保存 Excel 文件时出现运行时错误 1004(需要 VBA)

标签 vba excel

我想知道是否有人知道如何使用保存在 Excel 中打开的 .txt 文件?

我尝试使用用户窗体编写编码,但它给了我错误。

我想知道是否可以为用户提供将其保存在他/她最喜欢的位置以及他/她最喜欢的名称的选项?

 Public Sub CommandButton1_Click()
 Dim YesOrNoAnswerToMessageBox As String
 Dim QuestionToMessageBox As String
 Dim CurrentFile As String

 QuestionToMessageBox = "Do you want to save?"

YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "Save file")

If YesOrNoAnswerToMessageBox = vbNo Then
     Unload Me 'Cancellation command
Else
CurrentFile = ThisWorkbook.FullName
ActiveWorkbook.SaveAs "C:\myfile.xls", FileFormat:=52
Workbooks.Open CurrentFile
End If
End Sub

最佳答案

该错误是因为您的文件扩展名 (xls) 与您的文件类型 (OpenXMLWorkbookMacroEnabled) 不匹配。您需要 xlsm 扩展。

Sub Command1Click()

    Dim lResp As Long
    Dim sCurrFile As String
    Dim sNewFile As String

    Const sPROMPT As String = "Do you want to save?"
    Const sFILTER As String = "*.xlsm, *.xlsm"

    lResp = MsgBox(sPROMPT, vbYesNo, "Save File")

    If lResp = vbYes Then
        sCurrFile = ActiveWorkbook.FullName 'save current file name
        sNewFile = Application.GetSaveAsFilename(, sFILTER) 'get new file name
        If sNewFile <> "False" Then 'user didn't cancel
            ActiveWorkbook.SaveAs sNewFile, xlOpenXMLWorkbookMacroEnabled
            ActiveWorkbook.Close False 'close new file
            Workbooks.Open sCurrFile 'open previous text file
        End If
    Else
        Unload Me
    End If

End Sub

关于vba - 保存 Excel 文件时出现运行时错误 1004(需要 VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9751622/

相关文章:

vba - Excel VBA 除以 2 除以 60

excel - 如何修复 hh :mm format

excel - VBA 宏将错误的数据复制到单元格

excel - 当对相邻单元格进行更改时,自动填充单元格中的日期

python - 如何使用数据框创建不确定长度的Excel电子表格?

vba - 等待大文件在 Excel 中打开

json - 在 VBA 中发送 JSON POST 请求

excel - 将数据透视缓存的连接更改为现有连接

excel - 给定 A 列中的值,在 EXCEL 中的 B 列中找到最小值/最大值

excel - 从一个单元格中查找值,从该单元格附近的单元格复制值并将其粘贴到另一张表