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:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]P 18-24 Boys[/TD]
[TD]P 25-54 Boys[/TD]
[/TR]
</tbody>[/TABLE]

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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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