Group pivot items

joshcas

New Member
Joined
Jan 25, 2012
Messages
32
Hi,

I was wondering how can I group pivot items based on the name ?, e.g. I want to group all the pivot items that are not named US or CA or BR , I did a little research on the forum and I found a code that I can adapt but is not very elegant , so I'm wondering if you guys have a better suggestion.

Any help will be much appreciated :biggrin:

Code:
Sub test()
 Dim i As Integer
 Dim cell1 As String
 Dim cell2 As String
 Dim rng As Range

 With ActiveSheet.PivotTables("[YourPivotTableName]").PivotFields_("[YourFieldName]")
 For i = 1 To .PivotItems.Count
 If .PivotItems(i).Name Like "[YourItemName]" Then
 .PivotItems(i).LabelRange.Select
 cell1 = ActiveCell.Address
 ElseIf .PivotItems(i).Name Like "[YourItemName]" Then
 .PivotItems(i).LabelRange.Select
 cell2 = ActiveCell.Address
 Else
 End If
 Next i
 End With

 Set rng = Range(cell1, cell2)
 Selection.Group
 End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I'm having some troubles to adapt the code above , can you guys give me some light here ?, or I'm open for a different method


Code:
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Ship_To_Country")

 For i = 1 To .PivotItems.Count

 If .PivotItems(i).Name = "US" Or .PivotItems(i).Name = "CA" Or .PivotItems(i).Name = "BR" Or .PivotItems(i).Name = "MX" Then
' .PivotItems(i).LabelRange.Select

' cell1 = ActiveCell.Address
'MsgBox ("Default Regions")
 Else

' Group the pivotitem

'cell2 = pt.pf2.PivotItems(i).LabelRange
'MsgBox (.PivotItems(i).Name)
'.PivotItems(i).LabelRange.Select
'cell1 = ActiveCell.Address
' cell2 = ActiveCell.Address
'Set rng = Range(cell1, cell2)
'  Selection.Group
'' Selection.Group
'MsgBox (cell1)
' reg1 = pt.pf.PivotItems(i).LabelRange
' Union(reg1, reg2).Group
 End If
 Next i

' Union(cell1, cell2).Group

 End With
 
Upvote 0

Forum statistics

Threads
1,215,773
Messages
6,126,821
Members
449,340
Latest member
hpm23

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