遇到一例无法保存编辑后的 Excel 文件的问题

某个 .xlsx 文件编辑后按 ctrl + s 保存,存在如下提示——

保存“C:\Users\demo\Downloads\工作簿1.xlsx”时检测到错误。Microsoft Excel 可通过删除或修复某些功能来保存此文件。若要在新文件中进行修复,请单击“继续”。若要取消保存该文件,请单击“取消”。

做了如下尝试——

  • 另存为 .xlsm/.xlsb等,失败
  • 另存为 .xls 成功
  • 另存.xls 为 .xlsx 失败
  • .xls 文件编辑后,可以保存
  • 使用 wps 编辑或另存 .xlsx 成功
  • wps生成的 .xlsx 依旧存在上面的问题。

对于 Excel 文件,使用错误检查时,发现存在“未受保护的公式”和“公式不一致”的错误。
使用如下宏忽略错误

Sub BatchIgnoreUnprotectedFormulaAndInconsistentFormulaErrors()
    ' 声明变量
    Dim ws As Worksheet
    Dim cell As Range
    
    ' 关闭屏幕更新,加快运行速度
    Application.ScreenUpdating = False
    
    ' 遍历当前工作簿中的所有工作表
    For Each ws In ActiveWorkbook.Worksheets
        ' 遍历当前工作表中已使用的所有单元格
        For Each cell In ws.UsedRange
            ' 检查单元格是否存在“未受保护的公式”错误
            If cell.Errors.Item(xlUnlockedFormulaCells).Value = True Then
                ' 如果存在,则忽略此错误
                cell.Errors(xlUnlockedFormulaCells).Ignore = True
            End If
            ' 检查单元格是否存在“公式不一致”错误
            If cell.Errors.Item(xlInconsistentFormula).Value = True Then
                ' 如果存在,则忽略此错误
                cell.Errors(xlInconsistentFormula).Ignore = True
            End If
        Next cell
    Next ws
    
    ' 恢复屏幕更新
    Application.ScreenUpdating = True
    
    ' 提示完成
    MsgBox "已批量忽略所有工作表中的“未受保护的公式”和“公式不一致”错误!", vbInformation
End Sub


或者针对未受保护的公式所在的单元格进行锁定

Sub LockFormulasAndProtectSheets()
    ' 声明变量
    Dim ws As Worksheet
    Dim cell As Range
    Dim formulaCells As Range
    Dim hasPassword As Boolean
    Dim sheetPassword As String
    
    ' 关闭屏幕更新,加快运行速度
    Application.ScreenUpdating = False
    
    ' 遍历当前工作簿中的所有工作表
    For Each ws In ActiveWorkbook.Worksheets
        ' 检查工作表是否已受保护
        hasPassword = ws.ProtectContents
        
        ' 如果工作表受保护,尝试取消保护(无密码)
        If hasPassword Then
            On Error Resume Next
            ws.Unprotect "" ' 尝试用空密码取消保护
            If Err.Number <> 0 Then
                ' 如果取消保护失败,提示用户输入密码
                sheetPassword = InputBox("请输入工作表 '" & ws.Name & "' 的密码:", "工作表密码")
                If sheetPassword = "" Then
                    ' 用户取消输入,跳过此工作表
                    On Error GoTo 0
                    GoTo NextSheet
                End If
                ws.Unprotect sheetPassword
                On Error GoTo 0
            Else
                On Error GoTo 0
            End If
        End If
        
        ' 解锁所有单元格
        ws.Cells.Locked = False
        
        ' 查找所有包含公式的单元格
        On Error Resume Next
        Set formulaCells = ws.UsedRange.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0
        
        ' 如果找到公式单元格
        If Not formulaCells Is Nothing Then
            ' 锁定所有公式单元格
            formulaCells.Locked = True
            
            ' 忽略"未受保护的公式"错误
            For Each cell In formulaCells
                If cell.Errors.Item(xlUnlockedFormulaCells).Value = True Then
                    cell.Errors(xlUnlockedFormulaCells).Ignore = True
                End If
            Next cell
        End If
        
        ' 重新保护工作表(使用原密码或空密码)
        If hasPassword Then
            If sheetPassword <> "" Then
                ws.Protect Password:=sheetPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
            Else
                ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
            End If
        Else
            ' 如果原工作表未受保护,现在保护它(无密码)
            ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
        
        ' 重置密码变量
        sheetPassword = ""
        
NextSheet:
    Next ws
    
    ' 恢复屏幕更新
    Application.ScreenUpdating = True
    
    ' 提示完成
    MsgBox "已锁定所有公式单元格并保护工作表!" & vbCrLf & _
           "同时忽略了所有'未受保护的公式'错误。", vbInformation
End Sub

结果依旧无法保存编辑。起初还怀疑是 WPS 下编辑生成的 xlsx,在用 Excel 程序编辑时,有兼容性问题。
最后解压 ET 保存的 .xlsx 文件,发现有个 \xl\embeddings\oleObject1.bin 文件。 打开 xlsx,使用定位对象,或者选择窗格,删除可疑图片后,就可以保存了。

posted @ 2025-10-22 15:44  geyee  阅读(14)  评论(0)    收藏  举报