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: 0
  • res.png
    res.png
    23.4 KB · Views: 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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?
 

Zerrets

New Member
Joined
Jan 24, 2020
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
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")
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Zerrets

New Member
Joined
Jan 24, 2020
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,696
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Glad we could help. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,486
Messages
5,636,611
Members
416,929
Latest member
Nitil

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