Hi everyone, I'm running into the titular error when the program reaches the pivot table creation at the bottom of the code. I am opening a tab delimited file and need to have the program select all data on the sheet for the pivot (it will be running files of varying row lengths). Any advice?
Thanks!
Philip
Sub DataCleaningwiOpenandPivot()
'Open File
Dim FName As Variant
Dim wb As Workbook
FName = Application.GetOpenFilename
If FName <> False Then
Set wb = Workbooks.Open(FName)
End If
'Data Cleaning
Application.ScreenUpdating = False
Range("P:P,I:I,H:H,G:G,M:M,C:C").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
ActiveCell.FormulaR1C1 = "Department"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Location"
Range("C1").Select
ActiveCell.FormulaR1C1 = "MRN"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Apt Date"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Apt Time"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Provider"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Apt Type"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Status"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Schedule Date"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Scheduler"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Slot Type"
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K1").Select
ActiveCell.FormulaR1C1 = "Days Between Schedule and Apt Date"
Range("K2").Select
ActiveCell.FormulaR1C1 = "=RC[-7]-RC[-2]"
Range("K2").Select
Selection.NumberFormat = "General"
Selection.Copy
Range("J2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Range("A1").Select
'Med Relable
Dim LastRow As Long
Dim i As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("B" & i).Value = "CTR" Then
Range("A" & i).Value = "CTR2"
End If
Next i
'First Pivot
Dim PrevSheet As Worksheet
Set PrevSheet = ActiveSheet
Range("A1").CurrentRegion.Select
Sheets.Add.Name = "Sheet1"
PrevSheet.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=ActiveSheet.UsedRange, _
Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable3", _
DefaultVersion:=xlPivotTableVersion15
Bookmarks