Insert header in pivottitles

AnnetteTR

Board Regular
Joined
Aug 19, 2010
Messages
85
Hi
I am making a quite large macro to create a pivot table where the user selects the items to sort and make a sum of these:
Rich (BB code):
Sub OutputPrUnit()
'
'
'

Dim Pt As PivotTable
Dim Sortering As String
Dim Forstevalg As String
Dim Andetvalg As String

'Change to the sheet with data
Sheets("Data Ventilation output trial").Select

'move to top left cell
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select

' Name DataArea
Range(Selection.End(xlToRight), Selection.End(xlDown)).Name = "Items"

' Get choise from user
frmOutput.Show
' Three values from user
Sortering = frmOutput.cbxSortering.Text
Forstevalg = frmOutput.cbx1Vis.Text
Andetvalg = frmOutput.cbx2Vis.Text

' Create Pivot Caches
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="Items").CreatePivotTable tabledestination:="", TableName:="ItemList"

Set Pt = ActiveSheet.PivotTables("ItemList")
ActiveSheet.PivotTableWizard tabledestination:=Cells(3, 1)
Pt.AddFields RowFields:=Sortering 'Move the list heading to the row field
Pt.PivotFields(Sortering).Orientation = xlDataField ' Move the list heading to the data field
With Pt.PivotFields(Sortering)
.Orientation = xlRowField
.Position = 1
End With

Pt.AddDataField Pt.PivotFields(Forstevalg), "Sum of Forstevalg", xlSum
Pt.AddDataField Pt.PivotFields(Andetvalg), "Sum of Andetvalg", xlSum
End Sub

The makro works well, but the title of the Columns are "Sum of Forstevalg" and "Sum of Andetvalg" .
Instead of Forstevalg it should have been the value of the string Forstevalg, and the same for Andetvalg (The value the user have chosen)

What is the syntax for that?

Regards Annette
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You need:
Code:
Pt.AddDataField Pt.PivotFields(Forstevalg), "Sum of " & Forstevalg, xlSum
Pt.AddDataField Pt.PivotFields(Andetvalg), "Sum of " & Andetvalg, xlSum
 
Upvote 0
Solution

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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