No me cabe el fichero, así que escribo el código que he puesto:
Option Explicit
Sub CrearTDConCampoFiltro()
Dim WSD As Worksheet
Dim WSI As Worksheet
Dim PTCache As PivotCache
Dim PT As PIVOTTABLE
Dim PRange As Range
Dim FinalRow As Long, FinalCol As Long
Set WSD = Worksheets("Datos")
Set WSI = Worksheets("Informe")
For Each PT In WSI.PivotTables
PT.TableRange2.Clear
Next PT
Buenas noches,
me he quedado atascado intentando crear una Macro para generar de una base de datos automáticamente una tabla dinámica.
La cuestión es que quiero que en las filas aparezcan los siguientes datos:
- SAP_Format
- T358
- Lieferant Name
- T536
- TLW_Code_Wert
en las columnas quisiera que aparecieran los siguientes datos:
- "ATP_Bestand"
- Intransit
- T805
- T807
- Lieferrueckstand
- Bestellausstand
- KDR_Menge
Por último que se pudieran filtrar todos los datos por:
- T134
Yo había puesto así el código, pero me da error:
PT.AddFields RowFields:=Array("SAP_Format", "T358", "Lieferant Name", "TLW_Code_Wert"), _
ColumnFields:=Array("ATP_Bestand", "Intransit", "T805", "T807", "Lieferrueckstand", "Bestellausstand", "KDR_Menge", "Data"), _
PageFields:="T134"
No me cabe el fichero, así que escribo el código que he puesto:
Option Explicit
Sub CrearTDConCampoFiltro()
Dim WSD As Worksheet
Dim WSI As Worksheet
Dim PTCache As PivotCache
Dim PT As PIVOTTABLE
Dim PRange As Range
Dim FinalRow As Long, FinalCol As Long
Set WSD = Worksheets("Datos")
Set WSI = Worksheets("Informe")
For Each PT In WSI.PivotTables
PT.TableRange2.Clear
Next PT
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
Set PT = PTCache.CreatePivotTable(TableDestination:=WSI.Cells(4, 1), TableName:="PivotTable1")
PT.ManualUpdate = True
PT.AddFields RowFields:=Array("SAP_Format", "T358", "Lieferant Name", "TLW_Code_Wert"), _
ColumnFields:=Array("ATP_Bestand", "Intransit", "T805", "T807", "Lieferrueckstand", "Bestellausstand", "KDR_Menge", "Data"), _
PageFields:="T134"
With PT
.ShowTableStyleRowStripes = True
.TableStyle2 = "PivotStyleMediuem2"
.ColumnGrand = False
.RowGrand = False
.NullString = "0"
.RepeatAllLabels xlRepeatLabels
.TableRange2.Font.Size = 10
End With
PT.PivotFields("SAP_Format").Subtotals(1) = True
PT.PivotFields("SAP_Format").Subtotals(1) = False
PT.PivotFields("LFT Name").AutoSort Order:=xlDescending, Field:="SOP"
PT.ManualUpdate = False
Set PTCache = Nothing
WSI.Activate
Range("A2").Select
MsgBox ("Tabla Dinámica Creada con Éxito"), vbInformation, "Miquel"
End Sub
y esta es la tabla donde saco la información:
Gracias de antemano!