筛选数据透视表的ShowDetails

我正在寻找有效的方法来筛选ShowDetails的数据透视表ActiveCell。 我想将结果缩小到特定的列和行

说,原始数据(在数据透视表build立)有10列。 我只想显示两列的细节,我只想select具有特定数据的行,即在特定列中没有空单元格。

这是我的出发点:

 Sub ShowDetailsAndFilterResults() On Error Resume Next Dim PT As PivotTable Set PT = ActiveCell.PivotTable If Not PT Is Nothing Then Selection.ShowDetail = True End If End Sub 

对于SQL用户来说,很容易想象我的ShowDetails命令的解决scheme只能得到SELECT限定的结果,列出特定的列,并使用WHERE子句获得更多的行条件。

这里有一些应该帮助你通过的指针:

 Sub ShowDetailsAndFilterResults() Dim PT As PivotTable, _ WsPt As Worksheet, _ WsD As Worksheet On Error Resume Next Set PT = ActiveCell.PivotTable Set WsPt = ActiveSheet If Not PT Is Nothing Then ActiveCell.ShowDetail = True Set WsD = ActiveSheet With WsD '---Delete useless columns--- .Range("C:C,E:E,G:G,H:H").Delete Shift:=xlToLeft '---Or just Hide useless columns--- 'Range("C:C,E:E,G:G,H:H").EntireColumn.Hidden = True '---Scan rows to filter them--- For i = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1 If .Cells(i, 4) <> "test_value" Then '---place the code regarding the test you have to do--- Else '---lets say you want to keep the rows that are different of "test_value" on column 4 .Range("A" & i).EntireRow.Hidden = True '---Or delete the rows that doesn't fit your criteria--- '.Range("A" & i).EntireRow.Delete Shift:=xlUp End If Next i End With End If On Error GoTo 0 End Sub 

如果你对结果行的过滤严格less于3个标准,你可以使用AutoFilter方法,但是我发现它不那么灵活,所以一般情况下我喜欢使用像我这里出现的循环。

希望这对你有所帮助