jkwleisemann
New Member
- Joined
- May 31, 2016
- Messages
- 19
Well, I'm trying to create a pivot table from raw data on a weekly report (technically, a co-worker is, and I'm trying to help them debug the macro). Everything seems to be reformatting on the source report correctly, but the problem happens when I'm creating the pivot table. I'm sharing the code here, with a comment line that indicates where the Debug puts me. I'm not that familiar with creating pivot tables in VBA, so I'm hoping the gurus here will be more useful for my co-worker!
Code:
Sub Delta_Dental()
'
' Delta_Dental Macro
' Formatting for Weekly Delta Dental Invoice to get Pivot Table needed.
'
'
Range("M3").Select
ActiveCell.FormulaR1C1 = "AMOUNT PAID"
Range("M4").Select
Columns("K:K").EntireColumn.AutoFit
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K5").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],4)"
Range("K5").Select
Selection.Copy
Range("K5:K3418").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("K3").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "CY"
Range("K5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Columns("K:K").Select
Application.CutCopyMode = False
Selection.Cut
Range("X1").Select
ActiveSheet.Paste
Columns("K:K").Select
Selection.Delete Shift:=xlToLeft
Range("A3:W3").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A3:W10253").Select
Sheets.Add
'Error seems to happen here - result 1004, application/user defined error, so of course it's easy to identify!
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Worksheets(2).Range("R3C1:R10253C23"), Version:= _
xlPivotTableVersion15).CreatePivotTable TableDestination:=Worksheets(1).Cells(3, 1), _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("AMOUNT PAID")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("GROUP#")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("SUB")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("CY")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("AMOUNT PAID"), "Count of AMOUNT PAID", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of AMOUNT PAID")
.Caption = "Sum of AMOUNT PAID"
.Function = xlSum
.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("GROUP#")
.PivotItems("(blank)").Visible = False
End With
End Sub