Option Explicit
Function SheetExists(SheetName As String) As Boolean
' returns TRUE if the sheet exists in the active workbook
SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets(SheetName).Name) > 0 Then
SheetExists = True
Exit Function
End If
NoSuchSheet:
End Function
Sub NewPivotTable()
'
' NewPivotTable Macro
'
Dim PTSheet As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Variant
[COLOR=Red]'''Dim HeaderArray() As Variant, i As Integer, FinalColumn As Integer
'''Dim Header As Variant[/COLOR]
'Delete old "Pivot" worksheet
If SheetExists("Pivot") Then
Application.DisplayAlerts = False
Sheets("Pivot").Delete
Application.DisplayAlerts = True
End If
Set PTSheet = Worksheets.Add
PTSheet.Name = "Pivot"
Set PRange = Worksheets("temp").Range("A1:M147")
'147 Rows & 13 Columns w/ headers in row 1
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="'temp'!" & PRange.Address(, , xlR1C1))
Set PT = PTCache.CreatePivotTable(TableDestination:="'Pivot'!R3C1", _
TableName:="PivotTable1")
[COLOR=Red]'''FinalColumn = Worksheets("temp").Cells(1, Columns.Count).End(xlToLeft).Column
'''ReDim HeaderArray(FinalColumn)
'''For i = 1 To FinalColumn Step 1[/COLOR] [COLOR=Red]
''' If Worksheets("temp").Cells(1, i).Value = "PM1" Then
''' HeaderArray(i) = "PM"
''' Else
''' HeaderArray(i) = Worksheets("temp").Cells(1, i).Value
''' End If
''' 'MsgBox i & " " & HeaderArray(i)
'''Next i[/COLOR]
PT.ManualUpdate = True
[COLOR=Red]' ' 'PT.AddFields RowFields:=HeaderArray() ', PageFields:="EA"[/COLOR]
PT.AddFields RowFields:=Array("Co", "Rte", "PM1", "Bridge Number", _
"Structure Name", "Work Description", "Estimated Cost"), _
PageFields:="EA"
ActiveWorkbook.ShowPivotTableFieldList = False
PT.ShowDrillIndicators = False
PT.DisplayFieldCaptions = True
With PT
.ColumnGrand = False
.RowGrand = False
.InGridDropZones = True
.AllowMultipleFilters = True
.RowAxisLayout xlTabularRow
End With
With PT.PivotCache
.RefreshOnFileOpen = True
.MissingItemsLimit = xlMissingItemsNone
End With
PT.ManualUpdate = False
PT.ManualUpdate = True
Columns("A:A").ColumnWidth = 6
Columns("B:B").ColumnWidth = 4
Columns("C:C").ColumnWidth = 6
Columns("D:D").ColumnWidth = 8
Columns("E:E").ColumnWidth = 25
Columns("F:F").ColumnWidth = 50
Columns("G:G").ColumnWidth = 10
End Sub