find特定PivotItem的地址

我有RowField.Name的值和RowField.NamePivotItem.Name的值,我想突出显示特定的单元格。 但我似乎无法find一种方法,让我遍历每个可见的RowField。

理想情况下,我想要这样的东西:

 Sub LoopThroughPivotAndFindValue() dim pt as PivotTable, pi as PivotItem, pf as PivotField Set pt = wb.PivotTables("PivotTableNo1") For each pf in pt.PivotFields For each pi in pf.PivotItems If pf.Name = "Test" And pi.Name = "Value" Then 'Ideally here would be the output of the address within the sheet End If Next pi next pf End Sub 

最终我想在数据透视表中给特定的单元格着色,但我似乎无法find正确的方法。 没有我知道的索引属性。

任何人有一个想法?

不知道你的数据透视表是什么样子,我不能完全回答,但我可以告诉你,你要使用的是GetPivotData方法,你可以命名你想要得到的领域和项目,它会返回一个范围。

希望这个伎俩!

UDF也可以是有用的,这对你来说可能是一个好的开始:

 Sub test_Spurious() MsgBox LoopThroughPivotAndFindValue("Test", "Value") End Sub 

这里是function:

 Function LoopThroughPivotAndFindValue(ByVal PivotFieldName As String, ByVal PivotItemName As String) As String Dim pT As PivotTable, _ pI As PivotItem, _ pF As PivotField, _ FoundIt As Boolean FoundIt = False Set pT = ActiveSheet.PivotTables(1) For Each pF In pT.PivotFields For Each pI In pF.PivotItems If pF.Name = PivotFieldName And pI.Name = PivotItemName Then 'Ideally here would be the output of the address within the sheet On Error GoTo ErrHandler FoundIt = True LoopThroughPivotAndFindValue = pI.LabelRange.Address Exit For On Error GoTo 0 End If Next pI Next pF If FoundIt Then GoTo FreeObjects ErrHandler: LoopThroughPivotAndFindValue = "Nothing" FreeObjects: Set pT = Nothing End Function 

所以,我想说最好的方法是将结果存储到一个临时variables中,并检查它是否为Nothing (纯文本,而不是平行对象),然后使用该地址对其进行着色!