VBA - Pivot Tables - Populate Pivot with Changing column headers

fife8

New Member
Joined
Sep 26, 2013
Messages
30
Hello/Thanks In Advance.

I am trying to find a way to loop thru column headers so that I can use what is found in VBA code to sort into a Pivot Table.

Column headers could be a number of Age combinations:
P 18-24 BoysP 25-54 Boys

<tbody>
</tbody>

For example, I want to take any column header that contains text *Boys* - and do the below for me:

With ActiveSheet.PivotTables("PivotTable1").PivotFields("P 18-24 Boys")
.Orientation = xlPageField
.Position = 1
End With

With ActiveSheet.PivotTables("PivotTable1").PivotFields("P 25-54 Boys")
.Orientation = xlPageField
.Position = 1
End With


Thanks
Cathy
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Have answer -

' FIND THE VALUES AND PUT INTO SUM BOX
Set ws = ActiveWorkbook.Sheets("Sheet1")
lngCurrentCol = 1
blnFound = False
Do While ws.Cells(1, lngCurrentCol) <> ""


If InStr(ws.Cells(1, lngCurrentCol), "Boys") > 0 Then
blnFound = True
strText = ws.Cells(1, lngCurrentCol)
End If

If blnFound = True Then
blnFound = False
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(strText), "Count of " & strText, xlCount

With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of " & strText)
.Caption = "Sum of " & strText
.Function = xlSum
End With
End If


lngCurrentCol = lngCurrentCol + 1
Loop
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top