2

When I open an Excel 2007 workbook with macros, I get the following error:

Excel found unreadable content in {FILENAME}. Do you want to recover the contents of this workbook?

I wonder if it might be the following macro. I added the ActiveSheet.Unprotect portion and made changes to the .SaveAs TempFilePath & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum portion (though I don't remember exactly what they are.

This macro exports one sheet in the workbook, then unprotects it, copies and pastes values only and then closes it. This works fine but when I go save or reopen the main workbook I get the "unreadable" error.

'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
4

1 回答 1

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)
于 2013-11-07T06:29:50.330 回答