如何从工作表中删除公式但保留其计算值

时间:2013-12-19 01:17:01

标签: excel vba excel-vba

您能告诉我如何从工作表中删除所有公式但是在excel VBA中保留计算结果吗?

我有一张名为map的工作表,其中有很多计算列,现在我想删除所有这些公式,但仍保留结果以保存到新工作表中。

1 个答案:

答案 0 :(得分:17)

方式1 (由@rdhs提供)

Sub Sample()
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("DTMGIS")

    ws.UsedRange.Value = ws.UsedRange.Value
End Sub

方式2 Using Copy - PasteSpecial - Values

Sub Sample()
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("DTMGIS")

    With ws.UsedRange
        .Copy
        .PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    End With
End Sub

方式3 使用SpecialCells

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range

    Set ws = ThisWorkbook.Sheets("DTMGIS")

    On Error Resume Next
    Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0

    If Not rng Is Nothing Then
        rng.Value = rng.Value
    End If
End Sub