VBA Problem with .CurrentPage in pivot table

Palucci

Banned user
Joined
Sep 15, 2021
Messages
138
Office Version
  1. 365
Platform
  1. Windows
I would like to be able to select filtered items in this pivot table, such as CRD_RWG = 1. However, my code below shows me Application or object definied error. If I add .CurrentPage to ("CRD_RWG"). CurrentPage shows me that Run-time error 1004. Unable to set CurrentPage property of PivotField class

My part of code :

VBA Code:
  With sh_d.PivotTables(tbName1)
   With .PivotFields("CRD_RWG")
    .ClearAllFilters
     .Orientation = xlPageField
     .Position = 1
        .CurrentPage = 1

similiar cross post : VBA Application definied error in pivot table
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I started with your previous thread and there are a lot of errors in the code you posted there.
VBA problem with Applacation - definied in pivot table

I suggest you turn on Option Explicit, it would pick up a lot of the errors such as using lr when you have defined lr1 and tablename when you have defined tablename1.

In terms of the specific question in this thread you need this line
VBA Code:
  .Orientation = xlPageField

This should be a full working version of your code.

VBA Code:
Sub CreatePivot_v02()

    Dim tbName As String
    Dim lr1 As Long, ptver1 As Long
    Dim tbName1 As String
   
    Dim wbMe As Workbook
    Dim sh_s As Worksheet, sh_d As Worksheet
    Set wbMe = ActiveWorkbook
   
    Set sh_s = wbMe.Sheets("kredyty")
    lr1 = sh_s.Cells(Rows.Count, "A").End(xlUp).Row
    ptver1 = 6
    tbName1 = "Tabela przestawna"
    Sheets.Add after:=Sheets(Sheets.Count)
    Set sh_d = ActiveSheet
   
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=sh_s.Name & "!R1C1:R" & lr1 & "C12", Version:=ptver1).CreatePivotTable _
        TableDestination:=sh_d.Name & "!R3C1", TableName:=tbName1, _
        DefaultVersion:=ptver1

    With sh_d.PivotTables(tbName1).PivotFields("CRD_RWG")
        .Orientation = xlPageField
        .Position = 1
        .ClearAllFilters
        .CurrentPage = 1
    End With
   
    With sh_d.PivotTables(tbName1).PivotFields("NEW_DEFAULT")
        .Orientation = xlRowField
        .Position = 1
    End With
   
    With sh_d.PivotTables(tbName1)
        .AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
        .AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
    End With

End Sub
 
Upvote 0
Solution
Awesome ! Thanks ! a Option should be on start my macro ?
 
Upvote 0
@Alex Blakenburg i have one more qeustion if I would like to switch options in xlPageField several times and post the xlSum results next to it, how should I do it. My code shows me the error "Application-definied or object definied error"
VBA Code:
  Set sh_s = wbMe.Sheets("kredyty")
    lr1 = sh_s.Cells(Rows.Count, "A").End(xlUp).Row
    ptver1 = 6
    tbName1 = "Tabela przestawna"
    Sheets.Add After:=Sheets(Sheets.Count)
    Set sh_d = ActiveSheet
   
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:=sh_s.Name & "!R1C1:R" & lr1 & "C60", Version:=ptver1).CreatePivotTable _
        TableDestination:=sh_d.Name & "!R3C1", TableName:=tbName1, _
        DefaultVersion:=ptver1

    With sh_d.PivotTables(tbName1).PivotFields("CRD_RWG")
        .Orientation = xlPageField
        .Position = 1
        .ClearAllFilters
        .CurrentPage = 1
    End With
   
    With sh_d.PivotTables(tbName1).PivotFields("NEW_DEFAULT")
        .Orientation = xlPageField
        .Position = 1
        .ClearAllFilters
        .CurrentPage = 1
    End With
   
    With sh_d.PivotTables(tbName1)
        .AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
        .AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
    End With
wbMe.Sheets("Arkusz4").Range("A5:B5").Copy
wbMe.Sheets("Arkusz4").Range("A7").PasteSpecial Paste:=xlPasteValues
    With sh_d.PivotTables(tbName1).PivotFields("CRD_RWG")
        .Orientation = xlPageField
        .Position = 1
        .ClearAllFilters
        .CurrentPage = 1.5
    End With
    With sh_d.PivotTables(tbName1).PivotFields("NEW_DEFAULT")
        .Orientation = xlPageField
        .Position = 1
        .ClearAllFilters
        .CurrentPage = 0
    End With
    With sh_d.PivotTables(tbName1)
        .AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
        .AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
    End With

wbMe.Sheets("Arkusz4").Range("A5:B5").Copy
wbMe.Sheets("Arkusz4").Range("A8").PasteSpecial Paste:=xlPasteValues
 
Upvote 0
You are not giving me much to go on.
Show me your error dialogue box and what line is highlighted in the code when it fails and you click on debug.
 
Upvote 0
The most likely cause of your error message is that the value you have in the line(s) CurrentPage = 0 or some value , does not exist in your data set.

Ideally you need to work out what you want the code to do if the filter value does not exist and write some code to handle it.
As a quick and dirty solution you can add On Error around the CurrentPage lines eg
VBA Code:
        On Error Resume Next
        .CurrentPage = 0
        On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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