它看起来像我得到了我自己的问题的答案。我支持通过pivotfield循环并跳过那些不需要的,然后在循环中更新点。
这里是什么,我做了一个样本:
Set WSpt = Sheets("PivotTablesSheet")
Set pt = WSpt.PivotTables("PivotTableName")
Set pf = pt.PivotFields("FieldName")
F = pt.PivotFields("FilterName").CurrentPage.Name 'if Needed
Vals = Cht.SeriesCollection(1).Values 'Values of the chart because you can not work with them directly :(
Set LR = WSpt.Cells(pf.DataRange.Row, pf.DataRange.Column) 'First Cell in Field DataRange
'loop through cells in FieldName DataRange
For i = 1 To pf.DataRange.Cells.Count
'Put in check to see if the cell that was currently being checked was part of the primary field or the secondary one (only needed because I had a multi-level field setup
On Error Resume Next
tmp = pt.PivotFields("Main").PivotItems(LR.Value)
If Err.Number = 0 Then
Q = LR.Value
Set LR = LR.Offset(1, 0)
i = i - 1
Else
RC = LR.Value
Set LR = LR.Offset(1, 0)
'change formula to get the number value based on if the "(All)" option was selected or if just some items in the table need to be counted
If F = "(All)" Then
'Save Results of CountIF in the NUM variable
Num = Application.WorksheetFunction.CountIfs(Sheets("DataSheet").Range(Sheets("DataSheet").ListObjects("DataTBL").ListColumns("Main").DataBodyRange.Address), Q, _
Sheets("Data").Range(Sheets("DataSheet").ListObjects("DataTBL").ListColumns("FieldName").DataBodyRange.Address), RC)
Else
Num = Application.WorksheetFunction.CountIfs(Sheets("Data").Range(Sheets("DataSheet").ListObjects("DataTBL").ListColumns("FilterName").DataBodyRange.Address), F, _
Sheets("Data").Range(Sheets("DataSheet").ListObjects("DataTBL").ListColumns("Main").DataBodyRange.Address), Q, _
Sheets("Data").Range(Sheets("DataSheet").ListObjects("DataTBL").ListColumns("FieldName").DataBodyRange.Address), RC)
End If
Cht.SeriesCollection(1).Points(i).DataLabel.Text = FormatPercent(Vals(i), 1) & " (" & Num & ")"
Cht.SeriesCollection(1).Points(i).DataLabel.Orientation = xlUpward
End If
Next i
End Sub
问题是,数据透视表和图表都需要在飞行中进行更新,这就是为什么我去VBA路线。 –
阅读失败。我为我的房子蒙羞。 – Bmo
不用担心,我在考虑从1到点数的循环,然后尝试从系列集合中的XValues获取数据可能会起作用,但我无法从XValues中获取正确读取的数据。 :( –