Sub QC_PostProcessing() Dim MainWorksheet As Worksheet Set MainWorksheet = ActiveWorkbook.Worksheets("Anàlisi de Riscos") Dim DataRange As Range Set DataRange = MainWorksheet.UsedRange 'Formateja com una taula Range("A1").Select ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = "Table1" 'Definir nou estil ActiveWorkbook.TableStyles.Add ("Table Style 1") With ActiveWorkbook.TableStyles("Table Style 1") .ShowAsAvailablePivotTableStyle = False .ShowAsAvailableTableStyle = True End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements( _ xlWholeTable).Borders(xlEdgeTop) .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements( _ xlWholeTable).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements( _ xlWholeTable).Borders(xlEdgeLeft) .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements( _ xlWholeTable).Borders(xlEdgeRight) .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements( _ xlWholeTable).Borders(xlInsideHorizontal) .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements(xlHeaderRow _ ).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorDark1 End With With ActiveWorkbook.TableStyles("Table Style 1").TableStyleElements(xlHeaderRow _ ).Interior .ThemeColor = xlThemeColorLight2 .TintAndShade = -0.249946592608417 End With 'Estableix nou estil a la taula ActiveSheet.ListObjects("Table1").TableStyle = "Table Style 1" ' Format condicional de Risc Columns("G:G").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=""A-Alt""" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = 0 .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.399945066682943 .PatternTintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=""B-Mig""" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = 0 .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False ' Format condicional del valor Nou o Canviat Columns("D:D").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=""Si""" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = 0 .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.399945066682943 .PatternTintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False ' Mida de la font de la columna Ruta mes petita Range("Table1[Ruta]").Select Selection.Font.Size = 10 ' Autofit de les columnes Cells.Select Cells.EntireColumn.AutoFit ' Oculta la columna "Cob." Columns("J:J").Select Selection.EntireColumn.Hidden = True ActiveWindow.DisplayGridlines = False ' Creació i configuració Pivot table de Cobertura ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Table1", Version:=xlPivotTableVersion12).CreatePivotTable TableDestination _ :="", TableName:="PivotTable2", DefaultVersion:= _ xlPivotTableVersion12 With ActiveSheet.PivotTables("PivotTable2").PivotFields("és Nou/Canviat") .Orientation = xlRowField .Position = 1 End With With ActiveSheet.PivotTables("PivotTable2").PivotFields("Risc") .Orientation = xlRowField .Position = 2 End With With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cob.") .Caption = "Cobertura" .Orientation = xlColumnField .Position = 1 End With ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _ "PivotTable2").PivotFields("Id."), "Sum of Id.", xlSum With ActiveSheet.PivotTables("PivotTable2").PivotFields("Sum of Id.") .Caption = "Count of Id." .Function = xlCount End With With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Id.") .Caption = "Requisit" .Calculation = xlPercentOfRow .NumberFormat = "0.00%" End With With ActiveSheet.PivotTables("PivotTable2") .ColumnGrand = False .HasAutoFormat = False .RowGrand = False End With ActiveSheet.PivotTables("PivotTable2").PivotFields("és Nou/Canviat").AutoSort _ xlDescending, "és Nou/Canviat" ActiveSheet.PivotTables("PivotTable2").PivotSelect "Risc[All]", xlLabelOnly + _ xlFirstRow, True With ActiveSheet.PivotTables("PivotTable2") .InGridDropZones = True .RowAxisLayout xlTabularRow End With ActiveSheet.PivotTables("PivotTable2").PivotFields("és Nou/Canviat").Subtotals _ = Array(False, False, False, False, False, False, False, False, False, False, False, False _ ) ' Canvia titols columnes "Si" i "No" On Error Resume Next ActiveSheet.PivotTables("PivotTable2").PivotFields("Cobertura").PivotItems("Si"). _ Caption = "% Req. amb proves" ActiveSheet.PivotTables("PivotTable2").PivotFields("Cobertura").PivotItems("No"). _ Caption = "% Req. sense proves" ' Mida columnes Columns("A:D").ColumnWidth = 18 ActiveSheet.Name = "Cobertura" ' Crea estil per a la pivot table ActiveWorkbook.TableStyles("PivotStyleLight16").Duplicate ("PivotStyleLight16 3") With ActiveWorkbook.TableStyles("PivotStyleLight16 3") .ShowAsAvailablePivotTableStyle = True .ShowAsAvailableTableStyle = False End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlHeaderRow).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorDark1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlHeaderRow).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorAccent1 .ThemeColor = xlThemeColorAccent1 .TintAndShade = -0.499984740745262 .PatternTintAndShade = 0.799951170384838 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlHeaderRow).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = -0.499984740745262 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlTotalRow).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorLight1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlTotalRow).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorAccent1 .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0.799981688894314 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlTotalRow).Borders(xlEdgeTop) .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.399975585192419 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlFirstColumn).Interior .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.799981688894314 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlRowStripe1).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorDark1 .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.149998474074526 .PatternTintAndShade = -0.149998474074526 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlColumnStripe1).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorDark1 .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.149998474074526 .PatternTintAndShade = -0.149998474074526 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlColumnStripe1).Borders(xlEdgeLeft) .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.249977111117893 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlColumnStripe1).Borders(xlEdgeRight) .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.249977111117893 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlSubtotalColumn1).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorDark1 .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.149998474074526 .PatternTintAndShade = -0.149998474074526 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlSubtotalRow1).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorLight1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlSubtotalRow1).Borders(xlEdgeTop) .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlSubtotalRow1).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlSubtotalRow2).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorLight1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlRowSubheading1).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorLight1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlRowSubheading1).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = -0.499984740745262 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlRowSubheading2).Font .FontStyle = "Bold" .TintAndShade = 0 .ThemeColor = xlThemeColorLight1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlPageFieldLabels).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorAccent1 .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0.799981688894314 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlPageFieldLabels).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.399975585192419 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlPageFieldValues).Interior .Pattern = xlSolid .PatternThemeColor = xlThemeColorAccent1 .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0.799981688894314 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlPageFieldValues).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.399975585192419 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlWholeTable).Borders(xlEdgeTop) .ThemeColor = xlThemeColorAccent1 .TintAndShade = -0.499984740745262 .Weight = 2 .LineStyle = 1 End With With ActiveWorkbook.TableStyles("PivotStyleLight16 3").TableStyleElements( _ xlWholeTable).Borders(xlEdgeBottom) .ThemeColor = xlThemeColorAccent1 .TintAndShade = -0.499984740745262 .Weight = 2 .LineStyle = 1 End With ' Aplica estil a la pivot table ActiveSheet.PivotTables("PivotTable2").TableStyle2 = "PivotStyleLight16 3" 'Configuració pivot table (II) ActiveWindow.SmallScroll Down:=-9 'ActiveSheet.PivotTables("PivotTable2").PivotSelect "'és Nou/Canviat'[Si]", _ ' xlDataAndLabel, True ActiveSheet.PivotTables("PivotTable2").ShowDrillIndicators = False ActiveWindow.DisplayGridlines = False ' Oculta columna req. sense cobertura ActiveSheet.PivotTables("PivotTable2").PivotFields("Cobertura").AutoSort _ xlAscending, "Cobertura" Columns("D:D").Select Selection.EntireColumn.Hidden = True ' Selecciona la fulla Analisi de Riscos Range("A1").Select ActiveWorkbook.Worksheets("Anàlisi de Riscos").Select Range("A1").Select End Sub