简体   繁体   English

使用vba取消选择数据透视表中的所有项目

[英]Deselect all items in a pivot table using vba

Can some quicly explain the way to deselect all items in a newly created pivot table so that I can go back and select only one or two items? 有些人可以解释一下如何取消选择新创建的数据透视表中的所有项目,以便我可以返回并只选择一个或两个项目吗? I tried the following: 我尝试了以下方法:

.PivotItems("(Select All)").Visible = False

Thanks. 谢谢。

This is probably the closest you can get to what you want: 这可能是你能得到你想要的最接近的东西:

Dim i As Long
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
    .PivotItems(i).Visible = False
Next

This will make the very first option the only selected option (assuming this is within a with that points to the pivotfield). 这将使第一个选项成为唯一选择的选项(假设它位于指向枢轴场的a中)。 If you know what you want before hand... modify accordingly. 如果你事先知道你想要什么...相应地修改。

I've found that looping through each data item takes a lot of time, what you can do if you want to filter on a single item in a pivot without looping through all items is use the following code: 我发现循环遍历每个数据项需要花费大量时间,如果要在不调整所有项目的情况下过滤枢轴中的单个项目,可以使用以下代码:

ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").ClearAllFilters
ActiveSheet.PivotTables("Your Pivot Name").PivotFields("Your Field Name").PivotFilters.Add _
    Type:=xlCaptionEquals, Value1:="Your string here" 

this is basically a label filter but it worked for me. 这基本上是一个标签过滤器,但它对我有用。

Check the following out. 检查以下内容。 Select data for specific field name. 选择特定字段名称的数据。 Please do note that you have to at least select one item by default. 请注意,默认情况下您必须至少选择一个项目。 And also do not forget that if you want to hide items, Only contiguous items in a PivotTable Field can be hidden. 并且不要忘记,如果要隐藏项目,则只能隐藏数据透视表字段中的连续项目。 Perhaps at page load, or worksheet open or any of your other sub trigger, you could select a particular items to be selected based on a specific field. 也许在页面加载,或打开工作表或任何其他子触发器时,您可以根据特定字段选择要选择的特定项目。 Then allow your code to proceed with anything else. 然后允许您的代码继续其他任何操作。

Sub specificItemsField()
Dim pf As PivotField
Dim pi As PivotItem
Dim strPVField As String

strPVField = "Field Name"
Set pt = ActiveSheet.PivotTables(1)
Set pf = pt.PivotFields(strPVField)
Application.ScreenUpdating = False
Application.DisplayAlerts = False

On Error Resume Next
    pf.AutoSort xlManual, pf.SourceName
     For Each pi In pf.PivotItems
         pi.Visible = True
     Next pi
    pf.AutoSort xlAscending, pf.SourceName

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub       

This is how I do for custom filter selection. 这就是我自定义过滤器选择的方法。 May be slower due to double looping. 由于双循环可能会变慢。

Dim toSelect(1 To 3) As String

toSelect(1) = "item1"
toSelect(2) = "item2"
toSelect(3) = "item3"


For Each pvItem In objField.PivotItems
    For Each st In toSelect
        If pvItem.Value = st Then
            pvItem.Visible = True
            Exit For
        Else
            pvItem.Visible = False
        End If
    Next
Next

Well. 好。

Because you have not how to hide all, because, always you need to have 1 item visible 因为你没有如何隐藏所有,因为,总是你需要有一个项目可见

I do this: 我这样做:

I start hiding the first field, and before go to the next, i show all fields i need visible, then, i go to the secont item, and hide, and again show all items i want, and so on. 我开始隐藏第一个字段,然后在进入下一个字段之前,我显示我需要的所有字段,然后,我转到secont项目,然后隐藏,再次显示我想要的所有项目,依此类推。 Then, always will be visible any field, and wont have error. 然后,任何领域始终可见,并且不会有错误。

After the loop, i again try to show all fields i want. 循环之后,我再次尝试显示我想要的所有字段。

With ActiveSheet.PivotTables("TablaD2").PivotFields("Entity") 使用ActiveSheet.PivotTables(“TablaD2”)。PivotFields(“Entity”)

    Dim i As Long

    For i = 1 To .PivotItems.Count

    .PivotItems(i).Visible = False

    .PivotItems("ARG").Visible = True
    .PivotItems("BRL").Visible = True
    .PivotItems("GCB").Visible = True
    .PivotItems("MEX").Visible = True
    Next
    .PivotItems("ARG").Visible = True
    .PivotItems("BRL").Visible = True
    .PivotItems("GCB").Visible = True
    .PivotItems("MEX").Visible = True



End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM