Hi,
Working on a macro to create a from a database.
- After I made the pivot table on a new sheet I want to change the name of the sheet. How do I add this?
- Right now the macro stops at TableDestination row in the code, why ?
- Will the names I gave for the pivotfields (1,5,6) ensure that these columns are added from the raw data ? (the names of the column is the raw data is not 1,5,6 but Supplier, source and so on).
Sub test_export_pivot()
Dim PTcache As PivotCache
Dim PT As PivotTable
'Create the cache
Set PTcache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Range("A2").CurrentRegion)
'Add worksheet
Worksheets.Add
'Create pivottable
Set PT = ActiveSheet.PivotTables.Add( _
PivotCache:=PTcache, _
TableDestination:=Range("A3"))
'Specifies the fields
With PT
.PivotFields(1).Orientation = xlPageField
.PivotFields(5).Orientation = xlRowField
.PivotFields(6).Orientation = xlRowField
.PivotFields(7).Orientation = xlDataField
.PivotFields(8).Orientation = xlDataField
.PivotFields(9).Orientation = xlDataField
.PivotFields(10).Orientation = xlDataField
.PivotFields(11).Orientation = xlDataField
.PivotFields(12).Orientation = xlDataField
.PivotFields(13).Orientation = xlDataField
.PivotFields(14).Orientation = xlDataField
.PivotFields(15).Orientation = xlDataField
.PivotFields(16).Orientation = xlDataField
.PivotFields(17).Orientation = xlDataField
.PivotFields(18).Orientation = xlDataField
.PivotFields(19).Orientation = xlDataField
.PivotFields(20).Orientation = xlDataField
.PivotFields(21).Orientation = xlDataField
.PivotFields(22).Orientation = xlDataField
.PivotFields(23).Orientation = xlDataField
.PivotFields(24).Orientation = xlDataField
.PivotFields(25).Orientation = xlDataField
.PivotFields(26).Orientation = xlDataField
.PivotFields(27).Orientation = xlDataField
.PivotFields(28).Orientation = xlDataField
.PivotFields(29).Orientation = xlDataField
.PivotFields(30).Orientation = xlDataField
.PivotFields(31).Orientation = xlDataField
.PivotFields(32).Orientation = xlDataField
.PivotFields(33).Orientation = xlDataField
.PivotFields(34).Orientation = xlDataField
.PivotFields(35).Orientation = xlDataField
.PivotFields(36).Orientation = xlDataField
.PivotFields(37).Orientation = xlDataField
.PivotFields(38).Orientation = xlDataField
.PivotFields(39).Orientation = xlDataField
.PivotFields(40).Orientation = xlDataField
.PivotFields(41).Orientation = xlDataField
.PivotFields(42).Orientation = xlDataField
'No field captions
.DisplayFieldCaptions = False
End With
End Sub
Appreciate your help!
Bookmarks