我有一个xslm文件。在VBA代码中,我想保存与xlsx和email相同的文件。我能将SaveCopy作为xls文件。如果我尝试将其保存为xlsx,它会被保存,但是当我打开它时,它会出错。这是我正在使用的代码。请建议
ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy") & ".xlsx"
尝试打开此文件时出现错误: Excel无法打开文件' ... path \ MyFileName.xlsx'因为文件格式或文件扩展名无效。验证文件是否已损坏且文件扩展名是否与文件格式匹配
答案 0 :(得分:1)
SaveCopyAs
不会更改文件类型。
您无法通过SaveCopyAs
将.xlsm保存为.xlsx。
修改强>
解决方法是保存一个副本,然后在类型中更改该副本,而旧副本将被删除,如:
Dim wb As Workbook, pstr As String
pstr = ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy") & ".xlsm"
ActiveWorkbook.SaveCopyAs Filename:=y
Set wb = Workbooks.Open(pstr)
wb.SaveAs Left(pstr, Len(pstr) - 1) & "x", 52
wb.Close False
Kill pstr
答案 1 :(得分:0)
试试这个:
Sub SaveAsXLSX()
ThisWorkbook.Save 'Optional
Application.DisplayAlerts = False
ThisWorkbook.SaveAs ActiveWorkbook.Path & "\MyFileName - " & Format(Date, "mm-dd-yyyy"), 51 '51 = xlsx
Application.DisplayAlerts = True
ThisWorkbook.Close 'Optional
End Sub
您需要做的只是SaveAs
并将文件格式更改为51
(xlsx
)
如果你想"保存副本" - SaveAs
几乎完全相同 - 不同之处在于您当前打开的文件会成为保存的文件,但如果您愿意,您只需重新打开旧文件即可。
您实际想要做的是SaveAs
不同的文件类型,因此请使用SaveAs
。
答案 2 :(得分:0)
我认为我的代码更具可读性。经过测试。
Sub SaveXlsmAsXlsx()
Dim wb As Workbook, Filenamepath As String, Filenameext As String, Filenameonly As String, Filepathonly As String
Application.DisplayAlerts = False
Filenamepath = ActiveWorkbook.FullName
Filenameext = ActiveWorkbook.Name
Filenameonly = Replace(Filenameext, ".xlsm", "")
Filepathonly = Replace(Filenamepath, ".xlsm", "")
Set wb = Workbooks.Open(Filenamepath)
'51 = xlsx
wb.SaveAs Filename:=Filepathonly & "_" & Format(Date, "mm-dd-yyyy"), FileFormat:=51
wb.Close True
'Kill- Best not to kill anyone, you might be sorry
ThisWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
答案 3 :(得分:0)
Public Sub XLSMtoXLSX(FaylAdi As String)
Dim FullPath As String
Dim wb As Workbook
MsgBox "YOU WILL GET A WARNING AFTER COMPLETED, PLEASE WAIT"
ThisWorkbook.Save
On Error GoTo XETA
'You can change the name of the folder path below
FullPath = "C:\kohne sistem\Excel\VBA\Anbar\temp\" & FaylAdi & ".xlsm"
ThisWorkbook.SaveCopyAs FullPath
Application.DisplayAlerts = False
Set wb = Workbooks.Open(FullPath)
wb.SaveAs Left(FullPath, Len(FullPath) - 1) & "x", 51
wb.Close False
Kill FullPath
Application.DisplayAlerts = True
MsgBox "COMPLETED CORRECTLY"
Exit Sub
XETA: MsgBox "THERE WAS A FAULT SOMEWHERE"
End Sub
<块引用>
Private Sub CommandButton1_Click()
Call XLSMtoXLSX(Date)
End Sub