Sub Macro1()
On Error GoTo ErrHandler:
Sheets("Procente").Delete
Sheets("Status").Delete
Sheets("Valori").Delete
Sheets("Status1").Delete
'find last row
Dim LastRow As Long
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
End If
' find last column
Dim LastColumn As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
Dim WSD As Worksheet
Set WSD = Sheets.Add
WSD.Name = "Status"
For Each PT In WSD.PivotTables
PT.TableRange2.Clear
Next PT
'pivot +chart procente
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="Sheet1!R1C1:R" & LastRow & "C" & LastColumn).CreatePivotTable TableDestination:=WSD. _
Cells(3, 1), _
TableName:="PivotTable1"
'ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.NullString = "0"
.SmallGrid = False
End With
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="DATA_STADIU", _
ColumnFields:="MOTIV", PageFields:=Array("STADIU_DOSAR", "PRODUS")
With ActiveSheet.PivotTables("PivotTable1").PivotFields("procent")
.Orientation = xlDataField
.NumberFormat = "0.00%"
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("MOTIV").AutoShow _
xlAutomatic, xlTop, 9, "Sum of procent"
'Sheet1.Visible = xlSheetHidden
ActiveSheet.Range("B5").Select
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("Status").Range("B5")
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.PivotLayout.PivotFields("PRODUS").CurrentPage = "Flexi Centralizat"
ActiveChart.PivotLayout.PivotFields("STADIU_DOSAR").CurrentPage = "Respins"
ActiveChart.Name = "Procente"
'adauaga macro chart_calculate() in noul chart creat
Dim LineNum As Long
Dim strProcLine As String
With ActiveWorkbook.VBProject.VBComponents(ActiveChart.CodeName).CodeModule
' adaugat procedura goala
'Here it stops
.CreateEventProc "Calculate", "Chart"
'MsgBox ok3
strProcLine = "Format_Chart"
' MsgBox ok
.InsertLines .ProcBodyLine("Chart_Calculate", 0) + 1, strProcLine
' MsgBox ok
End With
' MsgBox ok
Dim isrs As Integer
ActiveChart.ChartType = xlLineMarkers
For isrs = 1 To ActiveChart.SeriesCollection.Count
ActiveChart.SeriesCollection(isrs).ApplyDataLabels
ActiveChart.SeriesCollection(isrs).Smooth = True
ActiveChart.SeriesCollection(isrs).Border.Weight = xlThin
ActiveChart.SeriesCollection(isrs).MarkerSize = 5
ActiveChart.Legend.Font.Size = 6
ActiveChart.SeriesCollection(isrs).Border.ColorIndex = isrs
ActiveChart.SeriesCollection(isrs).DataLabels.Font.FontStyle = "Bold"
ActiveChart.SeriesCollection(isrs).DataLabels.Font.Size = 7
Next
Sheet1.Activate
Dim wsd1 As Worksheet
Set wsd1 = Sheets.Add
wsd1.Name = "Status1"
For Each PT In wsd1.PivotTables
PT.TableRange2.Clear
Next PT
'pivot +chartul valori
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="Sheet1!R1C1:R" & LastRow & "C" & LastColumn).CreatePivotTable TableDestination:=wsd1. _
Cells(3, 1), _
TableName:="PivotTable2"
'ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2")
.NullString = "0"
.SmallGrid = False
End With
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:="DATA_STADIU", _
ColumnFields:="MOTIV", PageFields:=Array("STADIU_DOSAR", "PRODUS")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("NR motiv")
.Orientation = xlDataField
.NumberFormat = "0"
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("MOTIV").AutoShow _
xlAutomatic, xlTop, 9, "Sum of NR motiv"
'Sheet1.Visible = xlSheetHidden
ActiveSheet.Range("B5").Select
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("Status1").Range("B5")
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.PivotLayout.PivotFields("PRODUS").CurrentPage = "Flexi Centralizat"
ActiveChart.PivotLayout.PivotFields("STADIU_DOSAR").CurrentPage = "Respins"
ActiveChart.Name = "Valori"
'adauaga macro chart_calculate() in noul chart creat
Dim strProcLine1 As String
With ActiveWorkbook.VBProject.VBComponents(ActiveChart.CodeName).CodeModule
' adaugat procedura goala
.CreateEventProc "Calculate", "Chart"
strProcLine1 = "Format_Chart"
.InsertLines .ProcBodyLine("Chart_Calculate", 0) + 1, strProcLine1
End With
ActiveChart.ChartType = xlLineMarkers
For isrs = 1 To ActiveChart.SeriesCollection.Count
ActiveChart.SeriesCollection(isrs).ApplyDataLabels
ActiveChart.SeriesCollection(isrs).Smooth = True
ActiveChart.SeriesCollection(isrs).Border.Weight = xlThin
ActiveChart.SeriesCollection(isrs).MarkerSize = 5
ActiveChart.Legend.Font.Size = 6
ActiveChart.SeriesCollection(isrs).Border.ColorIndex = isrs
ActiveChart.SeriesCollection(isrs).DataLabels.Font.FontStyle = "Bold"
ActiveChart.SeriesCollection(isrs).DataLabels.Font.Size = 7
Next
ErrHandler:
If Err.Number = 9 Then
Resume Next
End If
End Sub
Public Sub Format_chart()
Dim isrs As Integer
ActiveChart.ChartType = xlLineMarkers
For isrs = 1 To ActiveChart.SeriesCollection.Count
ActiveChart.SeriesCollection(isrs).ApplyDataLabels
ActiveChart.SeriesCollection(isrs).Smooth = True
ActiveChart.SeriesCollection(isrs).Border.Weight = xlThin
ActiveChart.SeriesCollection(isrs).MarkerSize = 5
ActiveChart.Legend.Font.Size = 6
ActiveChart.SeriesCollection(isrs).Border.ColorIndex = isrs
ActiveChart.SeriesCollection(isrs).DataLabels.Font.FontStyle = "Bold"
ActiveChart.SeriesCollection(isrs).DataLabels.Font.Size = 7
Next
End Sub