简体   繁体   English

使用Excel VBA隐藏数据透视表项

[英]Hide pivot table items using excel VBA

I want to check the Colum A and Column E value and then collapse the Pivot table based on the values. 我想检查Colum A和Column E的值,然后根据这些值折叠Pivot表。

myfile.Sheets(3).Select
Dim dd As Range
Dim ee As Range
Dim ff As String
     For Each ee In myfile.Sheets(3).Range("E1:E20000")
     For Each dd In myfile.Sheets(3).Range("A1:A20000")
         With ee
            If Left(dd.Value, 1) = "F" And ee.Value = "0.00" Or ee.Value = "(0.00)" Then ff = Left(dd.Value, 10)
            ActiveSheet.PivotTables(1).PivotFields("Ref 1").PivotItems(ff).ShowDetail = False
        End With
     Next
     Next

I think this where the error comes up. 我认为这是错误发生的地方。 Specially with the variable ff. 特别是变量ff。

ActiveSheet.PivotTables(1).PivotFields("Ref 1").PivotItems(ff).ShowDetail = False

Try putting your string in Quotes. 尝试将字符串放入引号中。 So like

ActiveSheet.PivotTables(1).PivotFields("Ref 1").PivotItems(""" & ff & """).ShowDetail = False

Also if FF does not get set it still trys to collapse the pivot. 同样,如果未设置FF,它仍然会尝试折叠支点。 So try putting that in an if statement so if FF <> "" then .....showdetails = false 因此,请尝试将其放入if语句中,以便if FF <> "" then .....showdetails = false

EDIT updated code which should work. 编辑更新的代码,它应该可以工作。

myFile.Sheets(3).Select
Dim i As Long
Dim ff As String

For i = 5 To 5000
    If Left(ActiveSheet.Range("A" & i).Value, 1) = "F" And ActiveSheet.Range("E" & i).Value = "0" Then
        ff = Left(ActiveSheet.Range("A" & i).Value, 10)
        If ff <> "" Then
            For j = 1 To ActiveSheet.PivotTables("PivotTable1").PivotFields("Ref 1").PivotItems.Count
                If ActiveSheet.PivotTables("PivotTable1").PivotFields("Ref 1").PivotItems(j).Name = ff Then
                    ActiveSheet.PivotTables("PivotTable1").PivotFields("Ref 1").PivotItems(j).ShowDetail = False
                End If
            Next
        End If

    End If
Next i

The pivotItems is looking for an Index. ivotItems正在寻找索引。 So I have looped through all of them to see if FF matches the name of them. 因此,我遍历了所有这些对象,以查看FF是否与它们的名称匹配。 If it does it will hide the details. 如果这样做,它将隐藏细节。

Let me know if this works. 让我知道这个是否奏效。

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

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