当我使用宏打开Excel 2007工作簿时,出现以下错误:
Excel在{FILENAME}中找到了不可读的内容。你想恢复这个工作簿的内容吗?
我想知道它是否可能是以下宏。我添加了ActiveSheet.Unprotect
部分并对.SaveAs TempFilePath & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum
部分进行了更改(虽然我不记得它们到底是什么。
此宏导出工作簿中的一个工作表,然后取消保护,仅复制和粘贴值,然后将其关闭。这工作正常,但当我保存或重新打开主工作簿时,我收到“不可读”的错误。
'Working in Excel 97-2013
Sheets("Calculation").Select
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
FileExtStr = ".xlsx": FileFormatNum = 51
End If
End With
'Change all cells in the worksheet to values if you want
With Destwb.Sheets(1).UsedRange
Application.CutCopyMode = False
ActiveSheet.Unprotect
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
'Save the new workbook and close it
TempFilePath = Sheets("Calculation").Range("N5").Value
TempFileName = Range("N4").Value
With Destwb
.SaveAs TempFilePath & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close SaveChanges:=False
End With
MsgBox "You can find the new file in " & TempFilePath
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
答案 0 :(得分:2)
错误就在这里
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
xls的文件格式为56
而不是-4143
这些是Excel 2007-2013中的主要文件格式:
50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)