VBA用于公式到过滤范围内的值

时间:2017-04-14 05:35:18

标签: excel vba excel-vba

有时,我需要在选择过滤范围时运行公式,然后将其转换为值,每次我想要copy然后special paste values时,我必须首先清除过滤器。我需要macro将公式转换为值而不清除过滤器,我还想使用快捷键进行此操作。

2 个答案:

答案 0 :(得分:0)

下面的代码将允许此操作,CTRL + M快捷键可用于此操作。编辑宏以指定此快捷键。

Sub PasteFilterValues()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    If Selection.Cells.Count = 1 Then

        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Else

        Dim rng As Range

        Set rng = Selection.SpecialCells(xlCellTypeVisible)

        For Each cl In rng

            cl.Copy
            cl.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False

        Next cl

        rng.Select

    End If

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.CutCopyMode = False

End Sub

答案 1 :(得分:0)

这应该比其他解决方案快

Option Explicit

Sub fla2values(rng As Range)
    Dim c As Range
    For Each c In rng.SpecialCells(xlCellTypeVisible)
        c.Value = c.Value
    Next c
End Sub

Sub test_fla2values()
    fla2values Selection
End Sub