Excel VBA代码导致XML表错误,需要修复

时间:2019-02-22 00:02:23

标签: excel xml vba repair

我为我的团队创建了一个合同成本预测工具,除了这个问题之外,它的工作非常出色!我已经隔离了导致错误的宏,但是我无法确定确切原因,也无法解决。

用户完成其第一个预测/预测的现金流量后,他们按下“锁定原始”按钮,该按钮运行“ LockOriginal”宏(包括在下面)。目的是复制包含该初始预测的工作表,以备将来参考(删除公式等,因此只是值)。用户将保存并关闭该文档,然后每月返回至该文档以进行新的费用预测。

但是,第一次打开文档时(这是“从模板中创建之后,锁定原始文档并保存/关闭文档的”之后”),Excel崩溃。用户可以获取Excel以加载文档的“修复”版本。维修状态:     修复的记录:/xl/tables/table2.xml部分中的表(表) 此后,一旦他们保存了文档,它就可以在余生中正常运行。

我对这些触发错误的操作进行编码的方式是否存在?

Sub LockOriginal()

    If MsgBox("This action cannot be undone. Are you certain you want to proceed?", vbYesNo) = vbNo Then Exit Sub

    Application.ScreenUpdating = False

    'Overwrite fomulas in Original/Baseline with current forecast values
    Sheets("Summary").Range("ForecastValuesOriginal").Value = Sheets("Summary").Range("ForecastValuesCurrent").Value
    Sheets("Summary").Range("ForecastValuesBaseline").Value = Sheets("Summary").Range("ForecastValuesCurrent").Value

    'Delete features for locking Original again
    ActiveWorkbook.Names("ForecastValuesOriginal").Delete
    Sheets("Forecast").Select
    ActiveSheet.Shapes.Range(Array("Lock Original Button")).Delete

    'Duplicate sheet, rename to "Original", and convert formulas to values
    Sheets("Forecast").Copy After:=Sheets(5)
    ActiveSheet.Name = "Original"
    ActiveSheet.Tab.ColorIndex = 48
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    With Selection.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "ORIGINAL FORECAST (LOCKED) - USE ONLY AS REFERENCE"
    Range("A1").Select
    Application.CutCopyMode = False

    'Delete named ranges in new worksheet
    Dim RangeName As Name
    On Error Resume Next
    For Each RangeName In Names
    ActiveSheet.Names(RangeName.Name).Delete
    Next
    On Error GoTo 0

    'Tidy up
    Range("A1").Select
    Sheets("Forecast").Select
    Range("A1").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = True

End Sub

0 个答案:

没有答案