Amber
Amber

Reputation: 944

How can I loop through the first column of a PivotTable?

I have a PivotTable which has two source columns (Country, City) as its row fields.

The part in red

I want to iterate through each of the cells in the row field items (the part in red). I tried:

For Each pf In pvt.DataBodyRange.Columns
  MsgBox pf.Value
Next

but couldn't get those names.

Can someone please help me?

Upvotes: 0

Views: 2149

Answers (1)

Tom
Tom

Reputation: 9878

Try using something like:

For Each pf In pvt.pivotfields("Country").DataRange
    MsgBox pf.Value
Next pf

Also using MsgBox Is going to create a load of pop ups. If you're just using this for debug consider using Debug.Print instead and watching in the Immediate Window (Ctrl + G) You won't have to keep dismissing the MsgBox

Update from Comment:

For Each pf in pvt.RowFields(1).DataRange
    Debug.Print pf.value
Next pf

Upvotes: 1

Related Questions