Sub FreqTables()
Application.ScreenUpdating = False
'This Procedure creates 34 pivot tables
Dim PTCache As PivotCache
Dim Pt As PivotTable
Dim SummarySheet As Worksheet
Dim ItemName As String
Dim Row As Long, Col As Long, i As Long
Application.ScreenUpdating = False
'Delete Summary Sheet if it Exists
'On Error Resume Next
'Application.DisplayAlerts = False
'Sheets("Frequency_Tables").delete
'On Error GoTo 0
'Add summary sheet
Set SummarySheet = Worksheets.Add
ActiveSheet.Name = "Frequency_Tables"
'Create Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Temp").Range("A1").CurrentRegion)
Row = 1
For i = 1 To 16
For Col = 1 To 6 Step 5 '2 colums
ItemName = Sheets("Temp").Cells(1, i)
With Cells(Row, Col)
.Value = ItemName
.Font.Size = 16
.ColumnWidth = 65
End With
'Create Pivot table
Set Pt = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, Tabledestination:=SummarySheet.Cells(Row + 1, Col))
'Add Fields
If Col = 1 Then 'Freq Tables
With Pt.PivotFields(ItemName)
.Orientation = xlDataField
.Name = "Frequency"
.Function = xlCount
End With
Else
With Pt.PivotFields(ItemName)
.Orientation = xlDataField
.Name = "Percent"
.Function = xlCount
.Calculation = xlPercentOfColumn
.NumberFormat = "0.0%"
'.PivotFilters.xlValueisGreaterThan DataField := "Metropolitan Statistical Area/Metropolitan Division" Value1 := 9
End With
'Pt.PivotFields(ItemName).PivotFilters.Add
End If
Pt.PivotFields(ItemName).Orientation = xlRowField
'PT.PivotFields("Applicant Race: 1").Orientation = xlColumnField
Pt.TableStyle2 = "Pivotstylemedium2"
Pt.DisplayFieldCaptions = False
If Col = 6 Then
'add data bars to the last column
Pt.ColumnGrand = False
End If
Next Col
Row = Sheets("Frequency_Tables").Range("A65536").End(xlUp).Row + 3
Next i
End Sub
|