forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPivotTableSample2
More file actions
64 lines (51 loc) · 1.88 KB
/
PivotTableSample2
File metadata and controls
64 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'Pivot table
sPtName = "PivotTable1"
sShName = "Temp"
sTargShName = ActiveSheet.Name
'get last cell address, check if the header is empty
colToCheck = Range(lastCellAdd).Column
checkHeaderVal:
If Cells(1, colToCheck).Value = "" Then
colToCheck = colToCheck - 1
GoTo checkHeaderVal
End If
dtaSource = sTargShName & "!R1C1:R" & Range(lastCellAdd).Row & "C" & colToCheck
'add new worksheet to pivot the table (make sure always after CommitAvailabilityDates)
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = sShName
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
dtaSource, Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:=sShName & "!R3C1", TableName:=sPtName _
, DefaultVersion:=xlPivotTableVersion12
Sheets(sShName).Select
Cells(3, 1).Select
'With ActiveSheet.PivotTables(sPtName).PivotFields("Part Name")
With ActiveSheet.PivotTables(sPtName).PivotFields("Models")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables(sPtName).PivotFields("Order ID")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables(sPtName).PivotFields("CommitWW")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables(sPtName).AddDataField ActiveSheet.PivotTables( _
sPtName).PivotFields("Allocated Qty"), "Sum of Allocated Qty", xlSum
With ActiveSheet.PivotTables(sPtName)
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
'paste as values
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Delete first three rows
Rows("1:3").EntireRow.Delete
'for blank A column fields, copy the cell above
Range(Cells(1, 1), Cells(Range(lastCellAdd).Row, 1)).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
'paste as values
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False