基于原始数量而不是%的自定义图表数据标签

时间:2014-04-22 18:18:37

标签: excel excel-vba charts pivot-table excel-charts vba

我有一个数据透视表,显示字段的百分比作为值,但我希望标签是%和%基于的运行数的组合。

电流:

Current

我想要的是什么:

What I would like

%编号很容易,因为当我遍历数据系列集时,我可以从中获取值,但我无法弄清楚如何获取()中的数字。

我可以对原始数据集(~7000行)执行countif来获取数字,但这意味着我需要两个行轴项的当前值。Axis items

当我循环遍历seriescollection点以获取轴字段中两个项的值时,有没有办法

E.G。当我在16.86点时,我可以获得“响应性”和“无评论/无响应”的值,因此我可以在()

中执行countif和数字

2 个答案:

答案 0 :(得分:0)

您可以单击标签,按=键,然后将其指向一个单元格,它将显示您指向它的单元格的值。您应该创建一个帮助单元格,以便按照您希望的方式格式化值。

答案 1 :(得分:0)

看起来我得到了自己问题的答案。我通过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