保存Excel文件的运行时错误1004(需要VBA)

use*_*868 3 excel vba excel-vba

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

我曾尝试用UserForm编写代码,但它给了我错误.

我想知道是否有可能让用户选择将它保存在他/她最喜欢的地方,还有他/她最喜欢的名字?

 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
Run Code Online (Sandbox Code Playgroud)

Dic*_*ika 7

该错误是因为您的文件扩展名(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
Run Code Online (Sandbox Code Playgroud)