VBA Problem with creating pivot table

Palucci

Banned user
Joined
Sep 15, 2021
Messages
138
Office Version
  1. 365
Platform
  1. Windows
Hello guys, I have a problem because I wanted to create a PivotTable with a macro where the rows will be RES_MAT and the values will be WYCENA_PLNL. However, I did it with macro recording and I'm afraid it will crash to me at some point, how do I modify it to correct VBA code. And how to make it create a table, e.g. in a new sheet ? Thanks !

VBA Code:
wbMe.Sheets("papiery").Activate
wbMe.Sheets("papiery").Range("D1:L1" & Columns.Count).End(xlDown).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "papiery!R1C4:R30C12", Version:=6).CreatePivotTable TableDestination:= _
        "papiery!R28C16", TableName:="Tabela przestawna3", DefaultVersion:=6
wbMe.Sheets("papiery").Select
    Cells(28, 16).Select
    With wbMe.Sheets("papiery").PivotTables("Tabela przestawna3").PivotFields("RES_MAT")
        .Orientation = xlRowField
        .Position = 1
    End With
    wbMe.Sheets("papiery").PivotTables("Tabela przestawna3").AddDataField ActiveSheet. _
        PivotTables("Tabela przestawna3").PivotFields("WYCENA_PLN"), _
        "Suma z WYCENA_PLN", xlSum
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this:

VBA Code:
Sub Macro1()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim lr As Long, ptver As Long
  Dim tbName As String
  
  Set sh1 = Sheets("papiery")
  lr = sh1.Range("D" & Rows.Count).End(3).Row
  ptver = 6                         'pivot table version
  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)
    With .PivotFields("RES_MAT")
      .Orientation = xlRowField
      .Position = 1
    End With
    .AddDataField .PivotFields("WYCENA_PLN"), "Suma z WYCENA_PLN", xlSum
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,834
Messages
6,121,873
Members
449,056
Latest member
ruhulaminappu

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