Help needed with Pivot table macro to remove data

G

Guest

Hi I have a macro that creates a pivot table but what I need if for each
column of data that does not include the word "Student" to be removed:

So starting at column b the first column header in the pivot table would be
in cell B3 and if this cell does not have the word Student within the text
string then that column needs to be removed!

Hope you understand? Regards Neil
 
D

Debra Dalgleish

You could loop through the column fields and their items:

Sub test()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim strHide As String
Set pt = ActiveSheet.PivotTables(1)
strHide = "student"

For Each pf In pt.ColumnFields
Debug.Print pf.Name
For Each pi In pf.PivotItems
Debug.Print pi.Name
If InStr(1, UCase(pi.Caption), UCase(strHide)) > 0 Then
pi.Visible = False
Else
pi.Visible = True
End If
Next pi
Next pf


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top