Problems creating a pivot table

Zerrets

New Member
Joined
Jan 24, 2020
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello, i have trouble generating a pivot table with a range of data, at first i thought that i had to convert the range of data into a table and then generate the pivot table will solve the problem but it didn't and when leaving the range of data without the table format, the table is empty too.

This is my code:

VBA Code:
Sub Create()

    Dim PCache As PivotCache
    Dim TDinamica As PivotTable
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
           
   
    'Dim tbl As ListObject
    'Dim rng As Range

   'Set rng = Range(Range("A1"), Range("A1").SpecialCells(xlLastCell))
    'Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes)
    'tbl.TableStyle = "TableStyleMedium15"
   ' With Range("A1")
    '.Parent.ListObjects.Add(xlSrcRange, Range(.End(xlDown), .End(xlToRight)), , xlYes).Name = "Table1"
  'End With

    On Error Resume Next
    Worksheets("TablaDinamica").Delete
   
    'Crear Hoja TablaDinamica
    Worksheets.Add(Before:=ActiveSheet).Name = "TablaDinamica"


    Set PCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, SourceData:="Tabla1")
   
   

    Set TDinamica = PCache.CreatePivotTable( _
    TableDestination:="TablaDinamica!R3C1", TableName:="Tabla dinámica1")


    With TDinamica.PivotFields("Request ID")
     .Orientation = xlRowField
     .Position = 1
    End With
   
   
    With TDinamica.PivotFields("Actual Cost")
     .Orientation = xlDataField
     .Position = 1
     .Function = xlSum
     .NumberFormat = "#,##0"
    End With

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
   
End Sub
 

Attachments

  • tabla.png
    tabla.png
    79.7 KB · Views: 28
  • res.png
    res.png
    23.4 KB · Views: 19

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
All your error messages are being suppressed, so you'll never know if something goes wrong. Amend the code to:

Rich (BB code):
    On Error Resume Next
    Worksheets("TablaDinamica").Delete
On Error Goto 0

then try it again.

Do you have a table called Tabla1?
 
Upvote 0
All your error messages are being suppressed, so you'll never know if something goes wrong. Amend the code to:

Rich (BB code):
    On Error Resume Next
    Worksheets("TablaDinamica").Delete
On Error Goto 0

then try it again.

Do you have a table called Tabla1?
The reference is not valid '1004':
VBA Code:
Set TDinamica = PCache.CreatePivotTable( _
    TableDestination:="TablaDinamica!R3C1", TableName:="Tabla dinámica1")

And yes, this piece of code creates the pivot cache and changes the name of the table that has the data into "Tabla1"
VBA Code:
Set PCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, SourceData:="Tabla1")
 
Upvote 0
And yes, this piece of code creates the pivot cache and changes the name of the table that has the data into "Tabla1"
No, it doesn't. It tries to create a pivotcache from an existing range called Tabla1. If you don't have a table or range by that name, that would explain the 1004 error.
 
Upvote 0
You were right, silly mistake of me, i appreciate your help, all i had to do was rename the table at first so it would work.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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