transform Vertical information into horizontal - vba

I would like to have this transformation in vba
And to inderstend better i shere with you this picture
Now i have another column and i should have this information in the new table, but i don't find haw to make this?

All I did was setup your sample data and then record a macro and saved recorded macro generated below.
Now note: if your data is larger then you may want to record using key strokes to put cursor at beginning of table and then at the end of the table or "Define data as a table" and use the table.... but this gives you the general idea.
Sub Macro1()
' Macro1 Macro
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R10C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet1!R13C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(13, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer ")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Group"), "Sum of Group", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Price")
.Orientation = xlColumnField
.Position = 1
End With
Columns("C:C").ColumnWidth = 5.71
ActiveSheet.PivotTables("PivotTable1").PivotFields("Price").Subtotals = Array( _
False, True, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Group")
.Orientation = xlColumnField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Price"), "Sum of Price", xlSum
Range("E14").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Price").Caption = _
"Prix"
Range("B18").Select
ActiveSheet.PivotTables("PivotTable1").RowGrand = False
End Sub
Which then results in:
to handle the new column: Id combine the values in the raw data. if however you are after something different I would need to see an example of the desired output.

Related

Automatically update a pie chart

I have a sheet with lot of data, I create a pivot table to have a pie chart, I would like the pie chart to update if I filter on my sourcesheet data. I don't know if there's way to automatically update the pie chart while filtering on the source sheet. For example if I created a pivot table with 10 elements and that I return in my source sheet and apply a new filter on 5 element I would like the pie chart to be updated automatically with the new filter without making a new pivot table
Sub Macro1 ()
Cells.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Alarmes!R1C1:R1048576C11", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Graphe DOS!R1C1", TableName:="Tableau croisé dynamique2" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Graphe DOS").Select
Cells(1, 1).Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("'Graphe DOS'!$A$1:$C$18")
With ActiveChart.PivotLayout.PivotTable.PivotFields("Tranches")
.Orientation = xlPageField
.Position = 1
End With
'i'll be filtering on the SE column
With ActiveChart.PivotLayout.PivotTable.PivotFields("SE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("Alarmes")
.Orientation = xlRowField
.Position = 1
End With
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
PivotTable.PivotFields("Descriptifs"), "Nombre de Descriptifs", xlCount
End Sub
Thank You for your time and help

Creating a pivot table with certain criteria in VBA

When using my macro, the pivot table that already exists does not completely match the other sheet's information, so I am clearing out the pivot table sheet "TJC" and entering a new one from another sheet "TJ". I used a macro recorder to show exactly what I want in the pivot table. I am not sure how to get certain criteria for the table, but if you need more information besides this macro recorder, let me know.
isum.Sheets("TJ").Cells.Select
Selection.delete Shift:=xlUp
Range("H13").Select
Sheets("TJC").Select
Cells.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"TJCust!R1C1:R1048576C7", Version:=6).CreatePivotTable _
TableDestination:="TJCust!R1C1", TableName:="PivotTable1", _
DefaultVersion:=6
Sheets("TJCust").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable1").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
ActiveWorkbook.ShowPivotTableFieldList = True
Range("B10").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Item$SV$Item")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Hand_Amount"), _
"Count of Hand_Amount", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Count of Hand_Amount")
.Caption = "Sum of Hand_Amount"
.Function = xlSum
End With
In the code below a pivot table is created and named.
The xlRowFields are your categories (and sub categories).
The xlDataFields are your column data (and type of function on that data) for your categories. Then the Pivot table is sorted based on Column (DataField) values.
There is quite a bit of code exempted from this example, for instance there are new columns built finding averages for each pivot row, and a copy into pretty much a data table for easier formatting of the entire table before it is copied into a report (as a cell based table, not as a pivot any longer)
Option Explicit
'Added this code so you can see where the worksheet is coming from
'Use your own worksheet set up here
Dim CalcSheet as Worksheet
Set CalcSheet = ThisWorkbook.Worksheets("Calc Sheet")
'***************************************
'Make the Sales-Customer Pivot and table
'***************************************
'***************************
'Add Sales Pivot Table
'Also edited to place the Picot in cell A1 of the worksheet that you set up above
'Do not forget to modify the SourceData Range for your intended data range . . .
'Walk through this line by line so you understand what is happening
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
CalcSheet.Range("K14:AY" & LastDR), Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:=CalcSheet.Range("A1"), TableName:="SalesPVT", DefaultVersion _
:=xlPivotTableVersion15
With CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson")
.Orientation = xlRowField
.Position = 1
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("Customer")
.Orientation = xlRowField
.Position = 2
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("DD Rev")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "$#,##0"
End With
With CalcSheet.PivotTables("SalesPVT").PivotFields("Job Days")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
End With
CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson").AutoSort _
xlDescending, "Sum of DD Rev"
Here is how the Pivot table can be copied, dealing with them on reports is not that much fun when building further calculation columns off the data.
'copy pivot table to get rid of it
CalcSheet.PivotTables("SalesPVT").TableRange1.Copy
'Paste it as values with formatting
CalcSheet.Range("CA100").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
This is deleting the pivot table that was created after copy
CalcSheet.PivotTables("SalesPVT").TableRange1.Delete

Overwrite sheet if it exists

I recorded a macro that makes some pivot tables in a new sheet and it works fine, so i tried to make it able to overwrite if the sheet already existed but i can't seem to make it work. The problem is that when i use the macro once it does add the new sheet but it also adds another one on top of the one already made, and when i try to use the macro again to see if it overwrites the other one, it doesn't it just adds another unnamed sheet.
The code looks like this:
Sub Makro7()
Sheets.Add After:=ActiveSheet
On Error Resume Next
Sheets.Add().Name = "Statistics"
On Error GoTo 0
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Base!R1C1:R18288C12", Version:=6).CreatePivotTable TableDestination:= _
"Statistics!R1C1", TableName:="Pivottabel22", DefaultVersion:=6
Sheets("Statistics").Select
Cells(1, 1).Select
ActiveSheet.PivotTables("Pivottabel22").AddDataField ActiveSheet.PivotTables( _
"Pivottabel22").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel22").PivotFields("FACULTY_ID")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel22").PivotFields("PROGRAM_TYPE_NAME")
.Orientation = xlRowField
.Position = 1
End With
Range("A1").Select
ActiveSheet.PivotTables("Pivottabel22").DataPivotField.PivotItems( _
"Antal af FACULTY_ID").Caption = "Antal"
Range("B1").Select
ActiveSheet.PivotTables("Pivottabel22").CompactLayoutColumnHeader = "Fakultet"
Range("A7").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel22").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R7C1", TableName:= _
"Pivottabel23", DefaultVersion:=6
Sheets("Statistics").Select
Cells(7, 1).Select
ActiveSheet.PivotTables("Pivottabel23").AddDataField ActiveSheet.PivotTables( _
"Pivottabel23").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("PROGRAM_TYPE_NAME")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("FACULTY_ID")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("Antal af FACULTY_ID")
.Calculation = xlPercentOfTotal
.NumberFormat = "0.00%"
End With
Range("A7").Select
ActiveSheet.PivotTables("Pivottabel23").DataPivotField.PivotItems( _
"Antal af FACULTY_ID").Caption = "Procentvis"
Range("B7").Select
ActiveSheet.PivotTables("Pivottabel23").CompactLayoutColumnHeader = "Fakultet"
Range("A13").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel23").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R13C1", TableName:= _
"Pivottabel24", DefaultVersion:=6
Sheets("Statistics").Select
Cells(13, 1).Select
ActiveSheet.PivotTables("Pivottabel24").AddDataField ActiveSheet.PivotTables( _
"Pivottabel24").PivotFields("ENROLL_LOCATION_NAME"), _
"Antal af ENROLL_LOCATION_NAME", xlCount
With ActiveSheet.PivotTables("Pivottabel24").PivotFields("ENROLL_LOCATION_NAME" _
)
.Orientation = xlRowField
.Position = 1
End With
Range("B13").Select
ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems( _
"Antal af ENROLL_LOCATION_NAME").Caption = "Antal"
Range("A13").Select
ActiveSheet.PivotTables("Pivottabel24").CompactLayoutRowHeader = "Campus"
Range("B13").Select
ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems("Antal"). _
Caption = "Antal af studerende"
Range("A22").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel24").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R22C1", TableName:= _
"Pivottabel25", DefaultVersion:=6
Sheets("Statistics").Select
Cells(22, 1).Select
ActiveSheet.PivotTables("Pivottabel25").AddDataField ActiveSheet.PivotTables( _
"Pivottabel25").PivotFields("ENROLL_LOCATION_NAME"), _
"Antal af ENROLL_LOCATION_NAME", xlCount
With ActiveSheet.PivotTables("Pivottabel25").PivotFields("ENROLL_LOCATION_NAME" _
)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel25").PivotFields( _
"Antal af ENROLL_LOCATION_NAME")
.Calculation = xlPercentOfTotal
.NumberFormat = "0.00%"
End With
Range("A22").Select
ActiveSheet.PivotTables("Pivottabel25").CompactLayoutRowHeader = "Campus"
Range("B22").Select
ActiveSheet.PivotTables("Pivottabel25").DataPivotField.PivotItems( _
"Antal af ENROLL_LOCATION_NAME").Caption = "Procentvis af studerende"
Range("I1").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel25").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R1C9", TableName:= _
"Pivottabel26", DefaultVersion:=6
Sheets("Statistics").Select
Cells(1, 9).Select
ActiveSheet.PivotTables("Pivottabel26").AddDataField ActiveSheet.PivotTables( _
"Pivottabel26").PivotFields("STUDYBOARD_ID"), "Antal af STUDYBOARD_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel26").PivotFields("STUDYBOARD_ID")
.Orientation = xlRowField
.Position = 1
End With
Range("I1").Select
ActiveSheet.PivotTables("Pivottabel26").CompactLayoutRowHeader = "Studienævn"
Range("J1").Select
ActiveSheet.PivotTables("Pivottabel26").DataPivotField.PivotItems( _
"Antal af STUDYBOARD_ID").Caption = "Antal af studerende"
Range("L15").Select
End Sub
The problem is this piece of code:
Sheets.Add After:=ActiveSheet
On Error Resume Next
Sheets.Add().Name = "Statistics"
On Error GoTo 0
This basically tells Excel to add a new sheet after the active sheet and when you run the macro the second time, the sheetname "Statistics" is already taken. (And if it wasn't for On error resume next, an error message would appear the second time). Add this at the top of your macro instead:
Dim newSheet As Worksheet
Application.DisplayAlerts = False
Set newSheet = Sheets.Add(After:=ActiveSheet)
With newSheet
On Error Resume Next
ThisWorkbook.Sheets("Statistics").Delete
On Error GoTo 0
.name = "Statistics"
End With
Application.DisplayAlerts = True

Passing Excel Sheet name into macro VB

I think this should be a simple one considering I have very little VB knowledge :) I have recorded a macro to create a pivot table for a range on the current sheet. Of course what this gives me is the code to create a pivot table for that specific sheet. I am trying to adjust the code to grab the name of the active sheet so I can use this macro across different sheets. I have tried to search and find exactly how to retrieve the name and then supply that name to the code that will being the process of creating the pivot table.
I am hitting a roadblock at the ActiveWorkbook.PivotCaches.Create section of the code. I am getting the "Run-time error '1004': Application-defined or object-defined error" message.
Any help would be greatly appreciated!
Sub Macro1()
Dim sht As String
sht = ActiveSheet.Name
Columns("A:K").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Worksheets(sht).Range("!R1C1:R1048576C11"), Version:=6).CreatePivotTable TableDestination:= _
Worksheets(sht).Range("!R1C14"), TableName:="PivotTable3", DefaultVersion:=6
ActiveSheet.Select
Cells(1, 14).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Department")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"Originating Master Name")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Net")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Month")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Net"), "Count of Net", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Net")
.Caption = "Sum of Net"
.Function = xlSum
End With
End Sub
Change this
Worksheets(sht).Range("!R1C1:R1048576C11")
To this
ActiveWorkbook.Sheets(sht).Range("A1:K1048576")
And this
Worksheets(sht).Range("!R1C14")
To this
ActiveWorkbook.Sheets(sht).Range("N1")

Excel VBA - Stuck on generating pivot table from variable data range

I have the code below which theoretically should create a pivot table on a 2nd sheet (which exists) using the data it finds on the 'DATA' sheet. However it always crashes as soon as it reaches the part to create the pivot table.
I originally come from a fixed size and afterwards changed it so it should take all the data on my 'DATA' sheet regardless of whether it's a 2x3 or 58x13 table
Sheets("DATA").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "l"
Range("A1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Prior. per user!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Prior. per user").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
Range("D2").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority").PivotItems( _
"Medium").Position = 2
Columns("D:D").ColumnWidth = 7.43
Columns("C:C").ColumnWidth = 10
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Number"), "Sum of Number", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Number")
.Caption = "Count of Number"
.Function = xlCount
End With
If anyone sees what's wrong with it, it would be much appreciated.
Your target sheet name has spaces in it so you need to enclose it in single quotes:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Personally, I would also use a variable to refer to the pivot table:
Dim PT As PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14)
With PT
With .PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
.PivotFields("Priority").PivotItems("Medium").Position = 2
.Parent.Columns("D:D").ColumnWidth = 7.43
.Parent.Columns("C:C").ColumnWidth = 10
.AddDataField .PivotFields("Number"), "Count of Number", xlCount
End With