Kerry has attended:
Excel VBA Intro Intermediate course
Pivot tables - add % of row data field
I have created a pivot with TotalMembers data field (function = xlsum). I now want to duplicate the field but instead of showing sum I want to show values as % of row. I also want to re-name the original field to "Customers" and the duplicate data field to "% Customers".
I know how to do this using pivots but need to do it using VBA.
I have only just been on the intermediate course so my understanding is not great.
I created the pivot using the below:
Sub CreatePivot()
Set WSD_source = Worksheets("RAW_WeeklyBehaviour") ' sheet name of source data
' Add new sheet for pivot table
Sheets.Add after:=ActiveWorkbook.Sheets(Sheets.Count) 'uses count method of sheets collection to add to end
ActiveSheet.Name = "Pivot"
'create pivot
Set rngDestination = WSD_destination.Range("A1")
Set rngSource = WSD_source.Range("A1").CurrentRegion
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
SourceData:=WSD_source.Range("A1").CurrentRegion, tabledestination:=WSD_destination.Range("A1"), Tablename:="LostOpp"
Set PT = Sheets("Pivot").PivotTables("LostOpp")
'build pivot
With PT
.PivotFields("SearchSegmentLastWeek").Orientation = xlRowField
.PivotFields("SegmentChangeThisWeek").Orientation = xlColumnField
.PivotFields("TotalMembers").Orientation = xlDataField
.PivotFields("Total Customers").Function = xlSum
End With
With PT
.ColumnGrand = True
.RowGrand = False
.NullString = "0"
End With
End Sub
Thanks.
P.s. for trainer Stephen - please remember to email my course work to me.
RE: pivot tables - add % of row data field
Hi Kerry,
Thank you for your question and sorry about the delay in responding.
Try this code:
range("c4").select
With pvtTable
.PivotFields("Sum of Selling Price2")
.Calculation = xlPercentOfRow
.NumberFormat = "0.00%"
End With
Select a cell that contains one of the duplicate values.
Replace the field name with the one that has been generated.
I hope this helps.
Regards
Simon