I searched a long time for this, but still inconclusive. Say I have a pivot table with 2 row labels and 1 data variable looking like:
Rowlabel1 Rowlabel2 Sum of
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