VBA Problem with invalid or unqualifed reference

Palucci

Banned user
Joined
Sep 15, 2021
Messages
138
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a problem with the function that creates the pivot table, I wanted to select two filters "CRD_RWG" and "NEW_DEFAULT" and two items as values that will be the sum.
Here I got error invalid or unqualifed reference:
Rich (BB code):
End With
    .AddDataField .PivotFields

How to resolve it ?
This is my code :
Rich (BB code):
' tabel przestawna w kredytach dla nowych defaultow '
Set sh1 = wbMe.Sheets("kredyty")
  lr = sh1.Range("A" & Rows.Count).End(3).Row
  ptver = 6
  tbName = "Tabela przestawna"
  Sheets.Add after:=Sheets(Sheets.Count)
  Set sh2 = ActiveSheet
  ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=sh1.Name & "!R1C4:R" & lr & "C12", Version:=ptver).CreatePivotTable _
    TableDestination:=sh2.Name & "!R2C1", TableName:=tbName, _
    DefaultVersion:=ptver
  With sh2.PivotTables(tbName)
    .PivotFields ("CRD_RWG")
    .PivotFields ("NEW_DEFAULT")
      .Orientation = xlRowField
      .Position = 1
    End With
    .AddDataField .PivotFields("Suma z CRD_EKSP_PIER_DC_FIN"), "Sum", xlSum
    .AddDataField .PivotFields("Suma z CRD_KOR_DC_FIN"), "Sum", xlSum
  End With

cross post : VBA Problem with invalid or unqualifed reference in pivot table
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I have assumed that you want "CRD_RWG" as a row field. If it should be a column field instead, replace xlRowField with xlColumnField.

VBA Code:
    With sh2.PivotTables(tbName)
        With .PivotFields("CRD_RWG")
          .Orientation = xlRowField
          .Position = 1
        End With
        With .PivotFields("NEW_DEFAULT")
          .Orientation = xlRowField
          .Position = 2
        End With
        .AddDataField .PivotFields("Suma z CRD_EKSP_PIER_DC_FIN"), "Sum", xlSum
        .AddDataField .PivotFields("Suma z CRD_KOR_DC_FIN"), "Sum", xlSum
    End With

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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