Hi All,
I am using a csv file to create a PivotTable. The .csv file has more than 2 millions records (rows). Below is the code I have thus so far but however the PivotTable Fields are not showing after the code runs. It thus seems the code stopped doing the right thing from the highlighted line, i.e. where it start adding the pivot fields. I need help to figure out why it PivotFields are not showing up.
I am using a csv file to create a PivotTable. The .csv file has more than 2 millions records (rows). Below is the code I have thus so far but however the PivotTable Fields are not showing after the code runs. It thus seems the code stopped doing the right thing from the highlighted line, i.e. where it start adding the pivot fields. I need help to figure out why it PivotFields are not showing up.
Rich (BB code):
Sub RemoveAndCreateConnection()
Dim WS As Worksheet
Dim WB As Workbook
Set WB = ThisWorkbook
Set WS = WB.Worksheets("Report")
'RemoveAndCreateConnection
On Error Resume Next
ActiveWorkbook.Connections("DennisReport").Delete
'CSV Database Connection
'Workbooks("Dennis Weekly Report.xlsm").Connections.AddFromFile "C:\Users\Hashiru\Desktop\Report Templates\DennisReport.csv"
Workbooks("Dennis Weekly Report.xlsm").Connections.AddFromFile "C:\Users\Hashiru\Desktop\Report Templates\DennisReport.csv", True, False
'Create Pivot Cashe and PivotTable
Dim PC As PivotCache
Dim PT As PivotTable
Set PC = WB.PivotCaches.Create(SourceType:=xlExternal, SourceData:=ActiveWorkbook.Connections("DennisReport"), Version:=6)
Set PT = PC.CreatePivotTable(TableDestination:="Report!R3C1", TableName:="xLabor", DefaultVersion:=6)
PT.AddFields RowFields:="FAIN", ColumnFields:="Type", PageFields:="System Source"
PT.AddDataField Field:=PT.PivotFields("RMB Amount"), Function:=xlSum
With WS.PivotTables("xLabor").PivotFields("FAIN")
.Orientation = xlRowField
.Position = 1
End With
With WS.PivotTables("xLabor").PivotFields("Package")
.Orientation = xlRowField
.Position = 2
End With
WS.PivotTables("xLabor").PivotFields("Sum of RMB Amount").Caption = "RMB Amount ($'000)"
With WS.PivotTables("xLabor").PivotFields("System Source")
.Orientation = xlPageField
.Position = 1
End With
With WS.PivotTables("xLabor").PivotFields("Activity")
.Orientation = xlPageField
.Position = 1
End With
'Filter the PageField
With WS.PivotTables("xLabor")
.PivotCache.MissingItemsLimit = xlMissingItemsNone
'.PivotCache.Refresh
With .PivotFields("System Source")
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
End With
With .PivotFields("System Source")
.PivotItems("BAP").Visible = True
.PivotItems("BGL").Visible = True
.PivotItems("BTL").Visible = True
.PivotItems(1).Visible = False
End With
End With
'Filter ColumnField
With WS.PivotTables("xLabor")
.PivotCache.MissingItemsLimit = xlMissingItemsNone
'.PivotCache.Refresh
With .PivotFields("Type")
.PivotItems(1).Visible = True
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
End With
With .PivotFields("Type")
.PivotItems("INP").Visible = True
.PivotItems(1).Visible = False
End With
End With
'Filter the RowField
With WS.PivotTables("xLabor").PivotFields("FAIN")
.PivotItems("CELLULAR").Visible = False
.PivotItems("DEBT").Visible = False
End With
'Change PivotTable to Classic Style
With WS.PivotTables("xLabor")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
'Format the PivotTable
WS.PivotTables("xLabor").PivotFields("FAIN").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
Application.PivotTableSelection = False
WS.PivotTables("xLabor").PivotSelect "", xlDataOnly, True
Selection.NumberFormat = "#,##0_ );(#,##0)"
End Sub
Last edited: