2016-08-12 2 views
2

Ich bin nicht in der Lage, dies auszuführen, ich möchte Gesamtzeilen in Blatt zählen und übergeben, um Pivot-Diagramm zu erstellen.Wie debuggen Sie ein Pivot-Chart-Makro?

  1. Pivot Diagramm
  2. wählen fileds
  3. Klicken Sie doppelt Endsumme erstellen neues Blatt Ausbreitung
Sub Macro2() 

Dim ws As Worksheet 
Dim lastRow As Long 
Set ws = ActiveSheet 
NewSheet = ActiveSheet.Name 
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ 
    ws & "!R1C1:R" & lastRow & "C15",  
Version:=xlPivotTableVersion14).CreatePivotTable _ 
    TableDestination:=NewSheet & "!R1C1", TableName:="PivotTable1", 
DefaultVersion _ 
    :=xlPivotTableVersion14 

Sheets("NewSheet").Select 
Cells(1, 1).Select 
ActiveSheet.Shapes.AddChart.Select 
ActiveChart.ChartType = xlColumnClustered 
ActiveChart.SetSourceData Source:=Range("Sheet4!$A$1:$C$18") 
ActiveSheet.Shapes("Chart 1").IncrementLeft 192 
ActiveSheet.Shapes("Chart 1").IncrementTop 15 
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer") 
.Orientation = xlRowField 
.Position = 1 
End With 
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( 
_ 
"PivotTable1").PivotFields("Customer"), "Count of Customer", xlCount 
ActiveWindow.SmallScroll Down:=12 
Range("B29").Select 
Selection.ShowDetail = True 
End Sub' 
+0

Würden Sie uns durchsprechen, was dieser Code derzeit macht, und genauer, wo Sie gerade stecken bleiben? – halfer

+0

Ich möchte Pivot-Chat mit Macro erstellen, habe Makro aufgenommen und versucht, es auszuführen. Aber es funktioniert nicht. – Tim

+0

Was bedeutet "nicht arbeiten" konkret? Welche Leistung haben Sie erwartet und was haben Sie bekommen? – halfer

Antwort

1

Der folgende Code erstellen prüft die Daten in Sheet1 (ändern Sie zu Ihrem Blattnamen) und erstellt eine Pivot-Tabelle und ein Diagramm in Blatt Bericht.

Beim ersten Mal schafft es die Pivot-Tabelle und Diagramm von der zweiten Zeit, die die Pivot Cache gerade aktualisiert und die aktualisierten Datenzeilen (in Sheet1) und aktualisiert den Chart.

Sub Macro2() 

Dim sht1       As Worksheet 
Dim shtReport      As Worksheet 
Dim lastRow       As Long 
Dim PivotSrc_Range     As Range 

Dim PvtCache      As PivotCache 
Dim PvtTbl       As PivotTable 
Dim Chart1       As Chart 

' modify to your sheet name 
Set sht1 = ThisWorkbook.Sheets("Sheet1") 

' modify to your desired Pivot Table location 
Set shtReport = ThisWorkbook.Sheets("Report") 

' create the Source Range of the Pivot Cache 
lastRow = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row 

' it's looking uo tp Column "O" (15) as recorded in your MACRO 
Set PivotSrc_Range = sht1.Range(sht1.Cells(1, 1), sht1.Cells(lastRow, 15)) 

' set the Pivot Cache 
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotSrc_Range, Version:=xlPivotTableVersion14) 

On Error Resume Next 
Set PvtTbl = shtReport.PivotTables("PivotTable1") 

On Error GoTo 0 
If PvtTbl Is Nothing Then 
    ' create a new Pivot Table in "Report" sheet, start from Cell A2 
    Set PvtTbl = shtReport.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=shtReport.Range("A2"), TableName:="PivotTable1") 

    ' modify the name in brackets according to your Pivot Fields 
    With PvtTbl.PivotFields("Customer") 
     .Orientation = xlRowField 
     .Position = 1 
    End With   

    PvtTbl.AddDataField PvtTbl.PivotFields("Customer"), "Count of Customer", xlCount 

Else 
    ' just refresh the Pivot cache with the updated Range (data in Sheet1) 
    PvtTbl.ChangePivotCache PvtCache 
    PvtTbl.RefreshTable 
End If 

' check if already has a chart in sheet (from previous Macro Runs) 
If shtReport.ChartObjects.Count >= 1 Then 
    Set Chart1 = shtReport.ChartObjects(1).Chart 
Else ' first time >> create the chart 
    shtReport.Shapes.AddChart.Select 
    Set Chart1 = ActiveChart 
End If 

With Chart1 
    .ChartType = xlColumnClustered 
    .SetSourceData Source:=PvtTbl.TableRange1 ' refresh the chart with the updated Pivot Table 
End With 


End Sub 
+0

Danke Shai .. – Tim

+0

@SivaReddy du bist willkommen, als Antwort zu markieren –