Advertisement
fabianmct

bat01

Nov 14th, 2021
1,288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub PQueryPPivot()
  2.     '
  3.    ' Macro1 Macro
  4.    '
  5.    
  6.     '
  7.        'tabel comenzi
  8.        ActiveWorkbook.Queries.Add Name:="comenzi", Formula:= _
  9.             "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""C:\Users\user\OneDrive - EXCELp.ro\Desktop\_curs PowerExcel PORSCHE nov2021 (1)\_curs PowerExcel PORSCHE nov2021\aFiles\comenzi.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & "    Sheet1_Sheet = Source{[Item=""Sheet1"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & "    CapColoana = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    EliminareColoane" & _
  10.             " = Table.SelectColumns(CapColoana,{""Data comanda"", ""Data transport"", ""Client ID"", ""Canal"", ""Depozit"", ""Destinatie ID"", ""Regiune ID"", ""Cantitate"", ""Pret unitar"", ""Cost unitar""})," & Chr(13) & "" & Chr(10) & "    TipDate = Table.TransformColumnTypes(EliminareColoane,{{""Data comanda"", type date}, {""Data transport"", type date}, {""Client ID"", Int64.Type}, {""Canal"", type " & _
  11.             "text}, {""Depozit"", type text}, {""Destinatie ID"", Int64.Type}, {""Regiune ID"", Int64.Type}, {""Cantitate"", Int64.Type}, {""Pret unitar"", type number}, {""Cost unitar"", type number}})," & Chr(13) & "" & Chr(10) & "    PretLinie = Table.AddColumn(TipDate, ""Pret Linie"", each [Cantitate] * [Pret unitar], type number)" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    PretLinie" & _
  12.             ""
  13.         ThisWorkbook.Connections.Add2 "Query - comenzi", _
  14.             "Connection to the 'comenzi' query in the workbook.", _
  15.             "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=comenzi;Extended Properties=""""" _
  16.             , "SELECT * FROM [comenzi]", 2
  17.            
  18.         'tabel regiuni
  19.        
  20.         ActiveWorkbook.Queries.Add Name:="Regiuni", Formula:= _
  21.             "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.Workbook(File.Contents(""C:\Users\user\OneDrive - EXCELp.ro\Desktop\_curs PowerExcel PORSCHE nov2021 (1)\_curs PowerExcel PORSCHE nov2021\aFiles\baza.xlsx""), null, true)," & Chr(13) & "" & Chr(10) & "    Regiuni_Sheet = Source{[Item=""Regiuni"",Kind=""Sheet""]}[Data]," & Chr(13) & "" & Chr(10) & "    #""Promoted Headers"" = Table.PromoteHeaders(Regiuni_Sheet, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & "    #""Ch" & _
  22.             "anged Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""ID Regiune"", Int64.Type}, {""Localitate"", type text}, {""Judet"", type text}, {""Judet FD"", type text}, {""Cod judet"", type text}, {""Regiune"", type text}, {""Adresa"", type text}, {""Sector"", type text}, {""Populatie"", Int64.Type}, {""Desitate pop"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
  23.         ThisWorkbook.Connections.Add2 "Query - Regiuni", _
  24.             "Connection to the 'Regiuni' query in the workbook.", _
  25.             "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Regiuni;Extended Properties=""""" _
  26.             , "SELECT * FROM [Regiuni]", 2
  27.            
  28.         'merge tabele
  29.        ActiveWorkbook.Queries.Add Name:="Merge1", Formula:= _
  30.             "let" & Chr(13) & "" & Chr(10) & "    Source = Table.NestedJoin(comenzi, {""Destinatie ID""}, Regiuni, {""ID Regiune""}, ""Regiuni"", JoinKind.LeftOuter)," & Chr(13) & "" & Chr(10) & "    #""Expanded Regiuni"" = Table.ExpandTableColumn(Source, ""Regiuni"", {""Localitate"", ""Judet"", ""Judet FD"", ""Cod judet"", ""Regiune"", ""Adresa"", ""Sector"", ""Populatie"", ""Desitate pop""}, {""Localitate"", ""Judet"", ""Judet FD""" & _
  31.             ", ""Cod judet"", ""Regiune"", ""Adresa"", ""Sector"", ""Populatie"", ""Desitate pop""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Expanded Regiuni"""
  32.         ThisWorkbook.Connections.Add2 "Query - Merge1", _
  33.             "Connection to the 'Merge1' query in the workbook.", _
  34.             "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Merge1;Extended Properties=""""" _
  35.             , "SELECT * FROM [Merge1]", 2
  36.        
  37.         'pivot
  38.        ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
  39.             ActiveWorkbook.Connections("Query - Merge1"), Version:=7).CreatePivotTable _
  40.             TableDestination:="Sheet1!R1C1", TableName:="PivotTable1", DefaultVersion _
  41.             :=7
  42.         Cells(1, 1).Select
  43.         With ActiveSheet.PivotTables("PivotTable1")
  44.             .ColumnGrand = True
  45.             .HasAutoFormat = True
  46.             .DisplayErrorString = False
  47.             .DisplayNullString = True
  48.             .EnableDrilldown = True
  49.             .ErrorString = ""
  50.             .MergeLabels = False
  51.             .NullString = ""
  52.             .PageFieldOrder = 2
  53.             .PageFieldWrapCount = 0
  54.             .PreserveFormatting = True
  55.             .RowGrand = True
  56.             .SaveData = True
  57.             .PrintTitles = False
  58.             .RepeatItemsOnEachPrintedPage = True
  59.             .TotalsAnnotation = False
  60.             .CompactRowIndent = 1
  61.             .InGridDropZones = False
  62.             .DisplayFieldCaptions = True
  63.             .DisplayMemberPropertyTooltips = False
  64.             .DisplayContextTooltips = True
  65.             .ShowDrillIndicators = True
  66.             .PrintDrillIndicators = False
  67.             .AllowMultipleFilters = False
  68.             .SortUsingCustomLists = True
  69.             .FieldListSortAscending = True
  70.             .ShowValuesRow = False
  71.             .CalculatedMembersInFilters = False
  72.             .RowAxisLayout xlCompactRow
  73.         End With
  74.         With ActiveSheet.PivotTables("PivotTable1").PivotCache
  75.             .RefreshOnFileOpen = False
  76.             .MissingItemsLimit = xlMissingItemsDefault
  77.         End With
  78.         ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
  79.         'adauga coloane
  80.        ActiveSheet.PivotTables("PivotTable1").Name = "Pivot"
  81.         With ActiveSheet.PivotTables("Pivot").PivotFields("Canal")
  82.             .Orientation = xlRowField
  83.             .Position = 1
  84.         End With
  85.         ActiveSheet.PivotTables("Pivot").AddDataField ActiveSheet.PivotTables("Pivot"). _
  86.             PivotFields("Cantitate"), "Sum of Cantitate", xlSum
  87.         ActiveSheet.PivotTables("Pivot").AddDataField ActiveSheet.PivotTables("Pivot"). _
  88.             PivotFields("Pret Linie"), "Sum of Pret Linie", xlSum
  89.         'formatare numere
  90.        Columns("A:C").Select
  91.         Selection.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
  92.        
  93.         'adauga grafic
  94.        Range("A1").Select
  95.         ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
  96.         ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$C$5")
  97.         ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
  98.         ActiveChart.FullSeriesCollection(1).AxisGroup = 1
  99.         ActiveChart.FullSeriesCollection(2).ChartType = xlLine
  100.         ActiveChart.FullSeriesCollection(2).AxisGroup = 1
  101.         ActiveChart.FullSeriesCollection(1).ChartType = xlLine
  102.         ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
  103.         ActiveChart.FullSeriesCollection(1).AxisGroup = 2
  104.         ActiveSheet.Shapes("Chart 1").IncrementLeft -95.25
  105.         ActiveSheet.Shapes("Chart 1").IncrementTop -29.25
  106.         ActiveSheet.Shapes("Chart 1").IncrementLeft 330.75
  107.         ActiveSheet.Shapes("Chart 1").IncrementTop 24.75
  108.         Range("A2").Select
  109.         With ActiveSheet.PivotTables("Pivot").PivotFields("Regiune")
  110.             .Orientation = xlRowField
  111.             .Position = 2
  112.         End With
  113.         'schimba numele foi cu pivot
  114.        Sheets("Sheet1").Select
  115.         Sheets("Sheet1").Name = "Pivot"
  116.         'sorteaza pivot
  117.        ActiveSheet.PivotTables("Pivot").PivotFields("Regiune").AutoSort xlDescending, _
  118.             "Sum of Pret Linie", ActiveSheet.PivotTables("Pivot").PivotColumnAxis. _
  119.             PivotLines(2), 1
  120.         Range("C2").Select
  121.         ActiveSheet.PivotTables("Pivot").PivotFields("Canal").AutoSort xlAscending, _
  122.             "Sum of Pret Linie", ActiveSheet.PivotTables("Pivot").PivotColumnAxis. _
  123.             PivotLines(2), 1
  124.         'schimba locatia graficului
  125.        ActiveSheet.ChartObjects("Chart 1").Activate
  126.         ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Grafic"
  127.        
  128.         'adauga filtru in pivot
  129.        Sheets("Pivot").Select
  130.         With ActiveSheet.PivotTables("Pivot").PivotFields("Depozit")
  131.             .Orientation = xlPageField
  132.             .Position = 1
  133.         End With
  134.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").ClearAllFilters
  135.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").CurrentPage = "AXW291"
  136.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").ClearAllFilters
  137.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").CurrentPage = "FLR025"
  138.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").ClearAllFilters
  139.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").CurrentPage = "GUT930"
  140.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").ClearAllFilters
  141.         ActiveSheet.PivotTables("Pivot").PivotFields("Depozit").CurrentPage = "NXH382"
  142.        
  143.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement