Vba 列出第二行字段(多行字段)的excel数据透视项,在第一行字段中指定特定数据透视项

Vba 列出第二行字段(多行字段)的excel数据透视项,在第一行字段中指定特定数据透视项,vba,excel,Vba,Excel,我找了很长时间,但仍然没有结论。假设我有一个数据透视表,其中有两个行标签和一个数据变量,如下所示: Rowlabel1 Rowlabel2 Sum of value a A 1 B 2 C 3 d D 4 e E 5 我希望能够列出给定特定Rowlabel1的Rowlabel2的

我找了很长时间,但仍然没有结论。假设我有一个数据透视表,其中有两个行标签和一个数据变量,如下所示:

Rowlabel1 Rowlabel2 Sum of value
   a          A          1
              B          2
              C          3
   d          D          4
   e          E          5
我希望能够列出给定特定
Rowlabel1
Rowlabel2
的轴心项目,以在循环中获得
“a”“B”“C”
,然后将其合并到
“ABC”
。无论我尝试什么,它都只输出所有透视项,
“ABCDE”

由于实际数据更为复杂,因此不可能手动执行。它也有3行标签,而不是2个标签


不知道这是否有帮助,但解决方案也可以利用这些值。例如,返回值为
Rowlabel2
项。您可以将透视表用作常规表,并通过值以这种方式获取数据吗?但是,如果数据透视项被折叠,这将不起作用……您可以将数据透视表用作常规表,并以这种方式使用值获取数据吗?但是,如果数据透视项被折叠,这将不起作用…非常感谢,马克!它完美地解决了问题!降档可能是由于数据透视表的小计或样式,但解决方案是greatOkay,在我的项目中,因为我有3行标签,比如rowlabel0,我无法精确定位特定行Label0下特定行Label1的数据范围通过相交两个或多个范围解决了上述问题我很高兴它对您有效:-)您能接受我的答案吗?非常感谢,Mark!它完美地解决了问题!降档可能是由于数据透视表的小计或样式,但解决方案是greatOkay,在我的项目中,因为我有3行标签,比如rowlabel0,我无法精确定位特定行Label0下特定行Label1的数据范围通过相交两个或多个范围解决了上述问题我很高兴它对您有效:-)您能接受我的回答吗?
Sub getPiv()

Dim piv As PivotTable

'change this to whatever range and sheet names your pivottable is
Set piv = ThisWorkbook.Sheets("SecondTable_Output").Range("A3").PivotTable

Dim r As Range
Dim dr As PivotItem

'this captures your first entry in rowlabel1 - change as necessary
Set dr = piv.PivotFields(1).PivotItems(1)

Dim str As String
str = ""

'the macro won't work if the item is collapsed, so we set showDetail to true to expand it
If dr.ShowDetail = False Then
    dr.ShowDetail = True
End If

With dr
    'address what seems to be a bug(??) where if there are more than one data value columns,
    'the datarange appears to shift one row down
    If .DataRange.Columns.Count > 1 Then
        'here we shift back from the datarange to the 2nd column of the pivottable
        Set r = .DataRange.Offset(-1, -(.DataRange.Cells(1, 1).Column - 2))
    Else
        'here we shift back from the datarange to the 2nd column of the pivottable
        Set r = .DataRange.Offset(0, -(.DataRange.Cells(1, 1).Column - 2))
    End If
End With

'delete as necessary
r.Select

'obtain the string of cell values from the range
For i = 1 To r.Rows.Count
    str = str & " " & r.Cells(i, 1)
Next i
str = Trim(str)
MsgBox str

End Sub