Hello, I'm working on code that allows me to pick a file, and create 2 separate pivot tables off 2 separate worksheets from the file and updating additional worksheets with pivot table data and then repeating a second time with a 2nd file. When putting the code together separately and testing one part at a time I was able to create pivot tables just fine. Now that I've combined the code into one module I keep getting the following error from this line of code
I'm also not able to create them separately now any longer.
The prompt I receive is this: Run-time error ‘1004’: The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.
I've tried searching this but cannot find something that can help me fix my problem.
Complete code is below. Thanks in advance for any help you can provide. Hope this is something simple. I'm running Excel 2010 on Windows 7.
Code:
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
The prompt I receive is this: Run-time error ‘1004’: The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.
I've tried searching this but cannot find something that can help me fix my problem.
Complete code is below. Thanks in advance for any help you can provide. Hope this is something simple. I'm running Excel 2010 on Windows 7.
Code:
Sub RunAllMacros()
Pickfile
FD20Pivot1
FD20Pivot2
Pickfile2
ND2APivot1
ND2APivot2
End Sub
Sub Pickfile()
'Sets file types to display in file picker (this example is set for .xls files
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xlsx), *.xlsx", _
MultiSelect:=True, Title:="Workbook(s) to open")
'Section to enable file picker
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
Exit Sub
End If
x = 1
While x <= UBound(FilesToOpen)
Application.DisplayAlerts = False
Workbooks.Open Filename:=FilesToOpen(x)
x = x + 1
'Records selected workbooks name
thiswb = ActiveWorkbook.Name
'activate a workbook by stored variable name
Workbooks(thiswb).Activate
Wend
End Sub
Sub FD20Pivot1()
'creates FD20 DEF pivot table
Dim PCache As PivotCache, LastRow As Long, pt As PivotTable
Worksheets("DEF Data").Activate
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Sheet 1"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
'Select fields for PivotTable
ActiveWorkbook.ShowPivotTableFieldList = True
With pt.PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
With pt.PivotFields("GROUP")
.Orientation = xlRowField
.Position = 2
End With
pt.AddDataField pt.PivotFields("STAT"), "Count of STAT", xlCount
With pt.PivotFields("Count of STAT")
.Caption = "Count of STAT"
.Function = xlCount
End With
pt.AddDataField pt.PivotFields("Amount"), "Sum of Amount", xlCount
With pt.PivotFields("Sum of Amount")
.Caption = "Sum of Amount"
.Function = xlSum
End With
pt.AddDataField pt.PivotFields("Absolute"), "Sum of Absolute", xlCount
With pt.PivotFields("Sum of Absolute")
.Caption = "Sum of Absolute"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Name = "DEFPivot"
Rows("1:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'DEF status 1 calculation to report
ActiveSheet.PivotTables("PivotTable1").PivotFields("STATUS").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("STATUS").CurrentPage = "1"
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "DEFPivot"
Sheets("DEF Report").Select
Range("A9:E20").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet2"
Range("A1").Select
Application.CutCopyMode = False
Range("A2").Select
Sheets("DEF Report").Select
Range("A23:E34").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet3"
Range("A1").Select
Application.CutCopyMode = False
Range("A2").Select
Sheets("Sheet2").Select
Range("A1").Select
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C2:R20000C2,MATCH(RC[-1],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C3:R20000C3,MATCH(RC[-2],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C4:R20000C4,MATCH(RC[-3],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1:E12").Select
Selection.Copy
Sheets("DEF Report").Select
Range("A9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'DEF status 2 calculation to report
Sheets("DEFPivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("STATUS").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("STATUS").CurrentPage = "2"
Sheets("Sheet3").Select
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C2:R20000C2,MATCH(RC[-1],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C3:R20000C3,MATCH(RC[-2],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C4:R20000C4,MATCH(RC[-3],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1:E12").Select
Selection.Copy
Sheets("DEF Report").Select
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub FD20Pivot2()
'Reg 6 pivot
Worksheets("Reg 6 Data").Activate
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Sheet 4"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
'Select fields for PivotTable
ActiveWorkbook.ShowPivotTableFieldList = True
With pt.PivotFields("GROUP")
.Orientation = xlRowField
.Position = 1
End With
pt.AddDataField pt.PivotFields("Status Code"), "Count of Status Code", xlCount
With pt.PivotFields("Count of Status Code")
.Caption = "Count of Status Code"
.Function = xlCount
End With
pt.AddDataField pt.PivotFields("Amount"), "Sum of Amount", xlCount
With pt.PivotFields("Sum of Amount")
.Caption = "Sum of Amount"
.Function = xlSum
End With
pt.AddDataField pt.PivotFields("Absolute"), "Sum of Absolute", xlCount
With pt.PivotFields("Sum of Absolute")
.Caption = "Sum of Absolute"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Name = "REG6Pivot"
Rows("1:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
'REG6 calculation to report
Sheets("Sheet4").Select
Sheets("Sheet4").Name = "REG6Pivot"
Sheets("Reg 6 Report").Select
Range("B4:E4").Select
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Range("A4:E15").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet5"
Sheets("Sheet5").Select
Range("A1").Select
Range("B1:E2").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C2:R10000C2,MATCH(RC[-1],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C3:R20000C3,MATCH(RC[-2],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C4:R20000C4,MATCH(RC[-3],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("F1:I2").Select
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Range("A1:E12").Select
Selection.Copy
Sheets("Reg 6 Report").Select
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("DEF Report").Select
Range("C20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("C34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Sheets("Reg 6 Report").Select
Range("C15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E16").Select
Application.DisplayAlerts = False
Sheets("Sheet2").Select
ActiveSheet.Delete
Sheets("Sheet3").Select
ActiveSheet.Delete
Sheets("Sheet5").Select
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Pickfile2()
'Sets file types to display in file picker (this example is set for .xls files
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xlsx), *.xlsx", _
MultiSelect:=True, Title:="Workbook(s) to open")
'Section to enable file picker
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
Exit Sub
End If
x = 1
While x <= UBound(FilesToOpen)
Application.DisplayAlerts = False
Workbooks.Open Filename:=FilesToOpen(x)
x = x + 1
'Records selected workbooks name
thiswb = ActiveWorkbook.Name
'activate a workbook by stored variable name
Workbooks(thiswb).Activate
Wend
End Sub
Sub ND2APivot1()
'creates ND2A DEF pivot table
Worksheets("DEF Data").Activate
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Sheet 1"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
'Select fields for PivotTable
ActiveWorkbook.ShowPivotTableFieldList = True
With pt.PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
With pt.PivotFields("GROUP")
.Orientation = xlRowField
.Position = 2
End With
pt.AddDataField pt.PivotFields("STAT"), "Count of STAT", xlCount
With pt.PivotFields("Count of STAT")
.Caption = "Count of STAT"
.Function = xlCount
End With
pt.AddDataField pt.PivotFields("Amount"), "Sum of Amount", xlCount
With pt.PivotFields("Sum of Amount")
.Caption = "Sum of Amount"
.Function = xlSum
End With
pt.AddDataField pt.PivotFields("Absolute"), "Sum of Absolute", xlCount
With pt.PivotFields("Sum of Absolute")
.Caption = "Sum of Absolute"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Name = "DEFPivot"
Rows("1:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'DEF status 1 calculation to report
Sheets("DEF Report").Select
Range("A9:E20").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet2"
Range("A1").Select
Application.CutCopyMode = False
Range("A2").Select
Sheets("DEF Report").Select
Range("A23:E34").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet3"
Range("A1").Select
Application.CutCopyMode = False
Range("A2").Select
Sheets("Sheet2").Select
Range("A1").Select
Sheets("DEFPivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Status").ClearAllFilters
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Status")
.PivotItems("2").Visible = False
End With
Sheets("Sheet2").Select
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C2:R20000C2,MATCH(RC[-1],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C3:R20000C3,MATCH(RC[-2],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C4:R20000C4,MATCH(RC[-3],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1:E12").Select
Selection.Copy
Sheets("DEF Report").Select
Range("A9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'DEF status 2 calculation to report
Sheets("DEFPivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Status").ClearAllFilters
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Status")
.PivotItems("1").Visible = False
End With
Sheets("Sheet3").Select
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C2:R20000C2,MATCH(RC[-1],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C3:R20000C3,MATCH(RC[-2],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(DEFPivot!R1C4:R20000C4,MATCH(RC[-3],DEFPivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1:E12").Select
Selection.Copy
Sheets("DEF Report").Select
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub ND2APivot2()
'creates ND2A REG6 pivot table
Worksheets("Reg 6 Data").Activate
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Sheet 4"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("A1"), TableName:="PivotTable1")
'Select fields for PivotTable
ActiveWorkbook.ShowPivotTableFieldList = True
With pt.PivotFields("GROUP")
.Orientation = xlRowField
.Position = 1
End With
pt.AddDataField pt.PivotFields("Status Code"), "Count of Status Code", xlCount
With pt.PivotFields("Count of Status Code")
.Caption = "Count of Status Code"
.Function = xlCount
End With
pt.AddDataField pt.PivotFields("Amount"), "Sum of Amount", xlCount
With pt.PivotFields("Sum of Amount")
.Caption = "Sum of Amount"
.Function = xlSum
End With
pt.AddDataField pt.PivotFields("Absolute"), "Sum of Absolute", xlCount
With pt.PivotFields("Sum of Absolute")
.Caption = "Sum of Absolute"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Name = "REG6Pivot"
Rows("1:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'REG6 calculation to report
Sheets("Reg 6 Report").Select
Range("B4:E4").Select
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Range("A4:E15").Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "Sheet5"
Sheets("Sheet5").Select
Range("A1").Select
Range("B1:E2").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C2:R10000C2,MATCH(RC[-1],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 1).Copy
Cell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("C:C").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C3:R20000C3,MATCH(RC[-2],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 2).Copy
Cell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("D:D").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Set Rng = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).FormulaR1C1 = _
"=INDEX(REG6Pivot!R1C4:R20000C4,MATCH(RC[-3],REG6Pivot!R1C1:R10000C1,0))"
End If
Next
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For Each Cell In Rng
If Cell.Value <> "" Then
Cell.Offset(0, 3).Copy
Cell.Offset(0, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False = "1"
End If
Next
Columns("E:E").Select
Selection.Replace What:="#N/A", Replacement:="0", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("F1:I2").Select
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Range("A1:E12").Select
Selection.Copy
Sheets("Reg 6 Report").Select
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("DEF Report").Select
Range("C20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E20").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("C34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E34").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Sheets("Reg 6 Report").Select
Range("C15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("D15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E15").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-9]C:R[-1]C)"
Range("E16").Select
Application.DisplayAlerts = False
Sheets("Sheet2").Select
ActiveSheet.Delete
Sheets("Sheet3").Select
ActiveSheet.Delete
Sheets("Sheet5").Select
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub