PivotTable Fields not Showing

Hashiru

Active Member
Joined
May 29, 2011
Messages
286
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.

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:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,215,148
Messages
6,123,300
Members
449,095
Latest member
Chestertim

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