Error 1004 en tabla dinámica excel macro

Disculpe y de antemano Muchas gracias por la respuesta !

Pues mi problema es que cuando ejecuto la macro que creé para crear una tabla dinámica me aparece el runtime error 1004 "Error definido por la aplicación o el objeto"

Le anexo mi código enseguida

Sub main()
 Dim datos As Worksheet
 Dim tabla As Worksheet
 Dim Cache_Tabla As PivotCache
 Dim Tabla_Dinamica As PivotTable
 Dim tabla_range As Range
 Dim ultima_fila As Long
 Set tabla = Worksheets("Seguimiento PPTO")
 For Each Tabla_Dinamica In tabla.PivotTables
 Tabla_Dinamica.TableRange2.Clear
 Next
 Set datos = Worksheets("DATOS")
 ultima_fila = datos.Cells(Rows.Count, 3).End(xlUp).Row
 Set tabla_range = datos.Cells(1, 1).Resize(ultima_fila, 7)
 Sheets("DATOS").Select
 Set Cache_Tabla = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=tabla_range.Address, Version:=xlPivotTableVersion14)
 Set Tabla_Dinamica = Cache_Tabla.CreatePivotTable(TableDestination:=Worksheets("Seguimiento PPTO").Range("A7"), TableName:="Seguimiento PPTO", DefaultVersion:=xlPivotTableVersion14)
 Tabla_Dinamica.AddFields RowFields:=Array("Hotel", "Mes", "Segmento", "Subsegmento", "Homoclave", "A/D"), PageFields:="Clasif."
 With Tabla_Dinamica.PivotFields("CTOS: Real + Fcst")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Fcst. Meta")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Real + Libros")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Real + Libros vs Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Real + Libros vs Plan ( % )")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Real + Libros AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("CTOS: Cierre AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Real + Fcst")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Fcst. Meta")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Real + Libros")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Real + Libros vs Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Real + Libros vs Plan ( % )")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Real + Libros AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("INGR: Cierre AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Real + Fcst")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Fcst. Meta")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Real + Libros")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Real + Libros vs Plan (PP)")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Real + Librosvs Plan ( % )")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Real + Libros AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("%OCC: Cierre AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Real + Fcst")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Fcst. Meta")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Real + Libros")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Real + Libros vs Plan")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Real + Libros vs Plan ( % )")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Real + Libros AA")
 .Orientation = xlDataField
 .Function = xlSum
 .Position = 1
 .NumberFormat = "#'###,###.##"
 End With
 With Tabla_Dinamica.PivotFields("TP: Cierre...

1 Respuesta

Respuesta
1

Enviame la macro y los datos a mi correo, no sé en que instrucción está el error.

Mi correo es [email protected]

Disculpe por tardar en responder pero e estado ocupado.

Le envié un correo con los archivos Mi correo es [email protected]

Gracias de Antemano

Perdona la demora, espero aún tener una solución para tu pregunta:

El primer error está en le rango: no es columna 41, sino 40. Ahí te dice que el campo no es válido. Cambia por:

Set tabla_range = datos.Cells(1, 2).Resize(ultima_fila, 40)

No necesitas la instrucción del set Cache_tabla = Activeworkbook.PivotCaches...

Asigna directo a Tabla_Dinamica, de la siguiente forma.

Set Tabla_Dinamica = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ tabla_range.Address(ReferenceStyle:=xlR1C1), Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:=tabla.Range("A7"), TableName:="PPTO", DefaultVersion:=xlPivotTableVersion14)

A mi ya me funcionó, se tarda un buen tiempo en configurar todos los campos.

Si te sirvió la solución.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas