用值覆盖过滤后的 table

Overwrite filtered table with values

我正在尝试用自己的值覆盖过滤后的 table。使用此代码,我已经进行了过滤和复制,但它在粘贴时出错并显示此消息“范围 class 的 PasteSpecial 方法失败”。

ExpectedFeesTbl.Range.AutoFilter Field:=3, Criteria1:="F"
ExpectedFeesTbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
ExpectedFeesTbl.DataBodyRange.SpecialCells(xlCellTypeVisible).PasteSpecial xlPasteValues

我希望它做的是然后粘贴值。

这不起作用,因为您不能在非连续范围内粘贴。

解决方法是遍历可见单元格的区域,并用每个区域的值替换公式。

Dim VisibleCells As Range
Set VisibleCells = ExpectedFeesTbl.DataBodyRange.SpecialCells(xlCellTypeVisible)

If Not VisibleCells Is Nothing Then
    Dim iArea As Range
    For Each iArea In VisibleCells.Areas  ' loop through all areas
        iArea.Cells.Value = iArea.Cells.Value  ' convert all formulas in each area into values
    Next iArea
End If