2008-12-15 121 views
0

我在工作表中有一个数据集,每次都可能有所不同。我正在从这些数据创建一个数据透视表,但可能有一个数据透视图不在那里。例如:循环通过PivotItems:运行时错误91

.PivotItems("Administratie").Visible = False 

如果特定的值是不是在我的数据集,在VBA脚本失败,认为这不能在指定的字段定义的项目。 (错误1004)

所以我认为一个循环可能工作。 我有以下几点:

Dim pvtField As PivotField 
Dim pvtItem As PivotItem 
Dim pvtItems As PivotItems 

For Each pvtItem In pvtField.pvtItems 
     pvtItem.Visible = False 
Next 

但是,这给了我一个91错误在对于每个pvtItem行:

Object variable or With block variable not set 

我想我声明的变量不够好,但我最有可能失去了一些东西很明显...

回答

1

我知道了! :d

Dim Table As PivotTable 
Dim FoundCell As Object 
Dim All As Range 
Dim PvI As PivotItem 

    Set All = Worksheets("Analyse").Range("A7:AZ10000") 
    Set Table = Worksheets("Analyse").PivotTables("tablename") 
    For Each PvI In Table.PivotFields("fieldname").PivotItems 
     Set FoundCell = All.Find(PvI.Name) 
     If FoundCell <> "itemname" Then 
      PvI.Visible = False 
     End If 
    Next 

哇噢

由于MrExcel,答案那里毕竟,虽然深埋。

0

尝试这样:

Public Function Test() 
    On Error GoTo Test_EH 

    Dim pvtField As PivotField 
    Dim pvtItem As PivotItem 
    Dim pvtItems As PivotItems 

    ' Change "Pivot" to the name of the worksheet that has the pivot table. 
    ' Change "PivotTable1" to the name of the pivot table; right-click on the 
    ' pivot table, and select Table Options... from the context menu to get the name. 
    For Each pvtField In Worksheets("Pivot").PivotTables("PivotTable1").PivotFields 
     Debug.Print "Pivot Field: " & pvtField.Name 
     For Each pvtItem In pvtField.VisibleItems 
      pvtItem.Visible = False 
     Next 
    Next 

Exit Function 

Test_EH: 
    Debug.Print pvtItem.Name & " error(" & Err.Number & "): " & Err.Description 
    Resume Next 

End Function 

如果你想有一个功能,只测试一个支点项目的存在,你可以使用这样的事情:

Public Function PivotItemPresent(sName As String) As Boolean 
    On Error GoTo PivotItemPresent_EH 

    PivotItemPresent = False 

    For Each pvtField In Worksheets("Pivot").PivotTables("PivotTable1").PivotFields 
     For Each pvtItem In pvtField.VisibleItems 
      If pvtItem.Name = sName Then 
       PivotItemPresent = True 
       Exit Function 
      End If 
     Next 
    Next 

    Exit Function 

PivotItemPresent_EH: 
    Debug.Print "Error(" & Err.Number & "): " & Err.Description 
    Exit Function 

End Function 

你可以从你的代码中调用这个是这样的:

If PivotItemPresent("name_of_the_thing") Then 
    ' Do something 
End If 
0
For Each pvtField In Worksheets("my_sheet").PivotTables("my_table").PivotFields 
    For Each pvtItem In pvtField.PivotItems 
     Debug.Print vbTab & pvtItem.Name & ".Visible = " & pvtItem.Visible 
     /*.PivotItems(pvtItem).Visible = False*/ 
    Next 
Next 
.PivotItems("certain_Item").Visible = True 

这仍然不起作用...所有的变量仍然可见。没有显示任何错误,但它编译的值仍然存在。
我在那里添加的评论线是我自己的“发明”,但它是无效的。

编辑:Quicky问题:我可以使用IF语句来检查某个PivotItem是否实际位于数据透视表数据中?像

If PivotItem("name_of_the_thing") = present Then { 
    do_something() 
} 
0

东西,你不能说 “.PivotItems(pvtItem).Visible” 之外的 “With” 块。改为说“pvtField.PivotItems(pvtItem.Name).Visible = False”。

我还编辑了我的原始答案,以便在Excel无法设置Visible属性时包含错误处理。发生这种情况的原因是数据透视表至少需要一个行字段,一个列字段和一个数据项,因此每个数据项的最后一个不能隐藏。

当您尝试访问已隐藏的数据透视表项时,您也会遇到1004错误;我认为你可以忽略这些。

+0

对不起,我没有包括我所有的代码,我在这里发布的这个位被封装在一个With块中。感谢您的修改,我现在就试试。 :) – Kablam 2008-12-16 12:40:03

0

当我执行张贴帕特里克的代码, -

无法设置PivotItem类

的Visible属性 - 引发错误。

微软承认存在一个错误:M$ help
但是,只是隐藏线...是不是一个选项的课程。

+0

是否立即抛出此错误,或设置了其他项目后? Excel无法将所有项目的Visible属性设置为false,因为数据透视表至少需要一个行字段,一个列字段和一个可见的数据项。你为什么设置这些项目不可见?这解决了什么? – 2008-12-16 15:21:19

+0

答案在这里不适合,贴出如下:) – Kablam 2008-12-17 08:23:46

0

错误在循环结束时抛出。
我从帕特里克两个答案组合成以下几点:

With ActiveSheet.PivotTables("Table").PivotFields("Field") 

    Dim pvtField As Excel.PivotField 
    Dim pvtItem As Excel.PivotItem 
    Dim pvtItems As Excel.PivotItems 

    For Each pvtField In Worksheets("Sheet").PivotTables("Table").PivotFields 
     For Each pvtItem In pvtField.PivotItems 
      If pvtItem.Name = "ItemTitle" Then 
       pvtField.PivotItems("ItemTitle").Visible = True 
      Else 
       pvtField.PivotItems(pvtItem.Name).Visible = False 
      End If 
     Next 
    Next 
End With 

,如果该项目的特定字符串匹配,即项目设置为true。其他;项目设置为False。在False情况下,错误被抛出。
我知道True条件只有一个匹配。虽然当我'我F8'我的方式通过宏,真正的条件是从来没有输入...

而这解释了错误,一切都设置为假。 (谢谢帕特里克!)

引出我的疑问......究竟是什么 a PivotItem?



思想的事:
它解决了(或应该解决)以下:具有可变大小,其中,对于该特定的表,一列是感兴趣的一组数据。从那一栏我需要计算一个数值并把它放在一个表格中。表格中有一些条件,并且需要与另一列的组合,因此数据透视表是最佳解决方案。
问题是:在某些数据集中没有出现一个特定值。每次出现的值都不相同。

0

PivotItems是字段(列,行,数据)中的各个值。我认为它们是保存所有要汇总的单个数据项的“桶”。

而不是通过所有数据透视表字段(列,行和数据),你可以通过你感兴趣的领域。例如,这段代码将只显示指定的指定的透视项目现场:

Public Sub ShowInPivot(Field As String, Item As String) 
    On Error GoTo ShowInPivot_EH 

    Dim pvtField As PivotField 
    Dim pvtItem As PivotItem 
    Dim pvtItems As PivotItems 

    For Each pvtItem In Worksheets("Pivot").PivotTables("PivotTable1").PivotFields(Field).PivotItems 
     If pvtItem.Name = Item Then 
      pvtItem.Visible = True 
     Else 
      pvtItem.Visible = False 
     End If 
    Next 

    Exit Sub 

ShowInPivot_EH: 
    Debug.Print "Error(" & Err.Number & "): " & Err.Description 
    Exit Sub 

End Sub 

假设我有一个展示的每个客户发布的问题数的数据透视表和它们在我们的SDLC进行检测。 “客户”和“发布”是列字段,“阶段”是行字段。如果我想数据透视表限制计数问题的CustomerA,QA期间1.2版,我可以使用子上面这样的:

ShowInPivot "Customer", "CustomerA" 
ShowInPivot "Release", "1.2" 
ShowInPivot "Phase", "QA" 
+0

不工作,PivotItem.Name返回一个值,例如: “974”,而不是“itemname”... – Kablam 2008-12-19 10:03:45

0

当我尝试设置pivotitem可见true和false时,我也有相同的错误信息..这工作以前,但没有工作了...我比较两个值,并没有明确地将字符串更改为整数作比较..做这使得错误消失..

..所以如果你得到这个消息,检查是否有任何值进行比较,使项目可见或不正确比较..否则pivotitem为空,它可以不会让这个可见或不可见。

0

我有一个错误,说:“不能设置枢纽项目类有形财产” 在这一行:

For Each pi In pt.PivotFields("Fecha").PivotItems 
    If pi.Name = ffan Then 
     pi.Visible = True 
    Else 
     pi.Visible = False '<------------------------ 
    End If 
Next pi 

然后我读的互联网,我不得不进行排序手动,然后清除在缓存。我这样做,但错误仍然出现.....然后我读,这是因为我有该日期的枢轴场,所以我改变它冷杉我的colum普通数然后我希望设置可见的日期我将其更改为一般数量也是。那么没问题!!!!!!!!!!!!!! ....这里是....我希望这可以是有益的,因为我绝望!

Dim an As Variant 
an = UserForm8.Label1.Caption 'this label contains the date i want to see its the pivot item i want to see of my pivot fiel that is "Date" 
Dim fan 
fan = Format(an, "d m yyyy") 
Dim ffan 
ffan = Format(fan, "general number") 

Sheets("Datos refrigerante").Activate 'this is the sheet that has the data of the pivottable 
Dim rango1 As Range 
Range("B1").Select 
Range(Selection, Selection.End(xlDown)).Select 

Set rango1 = Selection 
ActiveSheet.Cells(1, 1).Select 
rango1.Select 

Selection.NumberFormat = "General" 'I change the format of the column that has all my dates 

'clear the cache 
Dim pt As PivotTable 
Dim ws As Worksheet 
Dim pc As PivotCache 

'change the settings 
For Each ws In ActiveWorkbook.Worksheets 
    For Each pt In ws.PivotTables 
     pt.PivotCache.MissingItemsLimit = xlMissingItemsNone 
    Next pt 
Next ws 

'refresh all the pivot caches 
For Each pc In ActiveWorkbook.PivotCaches 
    On Error Resume Next 
    pc.Refresh 
Next pc 

'now select the pivot item i want 
Dim pi As PivotItem 

Set pt = Sheets("TD Refrigerante").PivotTables("PivotTable2") 

'Sets Pivot Table to Manual Sort so you can manipulate PivotItems in PivotField 
pt.PivotFields("Fecha").AutoSort xlManual, "Fecha" 

'Speeds up code dramatically 
pt.ManualUpdate = True 

For Each pi In pt.PivotFields("Fecha").PivotItems 
    If pi.Name = ffan Then 
     pi.Visible = True 
    Else 
     pi.Visible = False 
    End If 
Next pi 

pt.ManualUpdate = False 
pt.PivotFields("Fecha").AutoSort xlAscending, "Fecha"