我想知道是否有人知道如何使用vba保存在 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/