首页 > 解决方案 > 用值替换数据透视表以过滤和删除行

问题描述

我正在尝试围绕来自数据透视表的一些数据构建电子表格,但我无法过滤和删除行。

我已经尝试.Value = .Value过桌子,但我收到了错误消息

“我们无法对选定的单元格进行此更改,因为它会影响数据透视表...”(运行时错误“1004”)

    With ws
        FinalRow = .Cells(Cells.Rows.Count, "B").End(xlUp).Row
        With .Range("B1:C" & FinalRow)'Pivot table range
            .Value = .Value
        End With

        For iCntr = FinalRow To 1 Step -1
            If Rows(iCntr).Hidden Then
                Rows(iCntr).EntireRow.Delete
            End If
        Next iCntr
    End With
End With

标签: excelvbapivot-table

解决方案


我认为你不能只是.Value这样的范围。您需要删除枢轴 - 可能通过存储数据、清除范围然后重新写入数据。

尝试这个:

Dim tmp As Variant
With ws
    FinalRow = .Cells(Cells.Rows.Count, "B").End(xlUp).Row
    With .Range("B1:C" & FinalRow) 'Pivot table range
        tmp = .Value
        .ClearContents
        .Value = tmp
    End With
End With

顺便说一句,如果您在数据透视表中“隐藏”了项目,它们会从表格中删除,而不是真正隐藏 - 所以我怀疑以后不需要删除..


推荐阅读