VBA Pivot Table bug does not show datafield value

chistos

New Member
Joined
Oct 10, 2020
Messages
8
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Heyy guys, I am struggling with problem. I have this code to create Pivot Table from data range:



VBA Code:
Sub test4()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim Lastrow As Long
Dim LastCol As Long



Set PSheet = Worksheets("Test 4")
Set DSheet = Worksheets("TB")


Lastrow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(Lastrow, LastCol)

On Error Resume Next

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        PRange, Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:=PSheet.Cells(14, 2), TableName:="Test 4", _
        DefaultVersion:=xlPivotTableVersion15

Sheets("Test 4").Activate

With ActiveSheet.PivotTables("Test 4").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
   
    ActiveSheet.PivotTables("Test 4").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("Test 4").PivotFields("AA")
        .Orientation = xlRowField
        .Position = 1
    End With

    Sheets("Test 4").Activate
    Sheets("Test 4").Select

[COLOR=rgb(97, 189, 109)]'This Data field does not show up[/COLOR]

    ActiveSheet.PivotTables("Test 4").AddDataField ActiveSheet. _
        PivotTables("Test 4").PivotFields("Turnover"), _
        "Sum of Turnover", xlSum

End Sub

I have tried several things, using variables and several types of code and always I struggle wit this bug, if anyone now how to tackle this, I will be grateful.
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Watch MrExcel Video

Forum statistics

Threads
1,129,441
Messages
5,636,305
Members
416,912
Latest member
danluk12

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
Top