I have the following code that show details of a filtered PivotTable in new worksheets. I want to create new PivotTable in these new worksheets and have the following code but I keep on running into the object required error on the Set Tbl = d.Parent.Cells(d.Row, lCol).Name line? How do I fix that error?
VBA Code:
Dim lCol As Long
Dim d As Range
Dim sRow As String
sRow = "Vendor Name"
Set d = Worksheets("various1").PivotTables("multiple_lines").DataBodyRange.Resize(, 1).SpecialCells(xlCellTypeVisible)
With Worksheets("various1").PivotTables("multiple_lines")
With .RowRange
On Error Resume Next
lCol = WorksheetFunction.Match(sRow, .Resize(1), 1)
On Error GoTo 0
If lCol = 0 Then
MsgBox "Rowfield Header: " & sRow & "not found."
Exit Sub
End If
lCol = .Column + lCol - 1
End With
For Each d In Worksheets("various1").PivotTables("multiple_lines").DataBodyRange.Resize(, 1).SpecialCells(xlCellTypeVisible)
If d.Parent.Cells(d.Row, lCol) <> "Grand Total" Then
d.ShowDetail = True
ActiveSheet.Name = Trim(Left(.Parent.Cells(d.Row, lCol), 20))
End If
' Declare Variables
Dim FSheet As Worksheet
Dim GSheet As Worksheet
Dim FCache As PivotCache
Dim FTable As PivotTable
Dim pvtFld As PivotField
Dim FRange As Range
Dim LastRowC As Long
Dim LastColC As Long
Dim Tbl As String
' Declare Variables
Set FSheet = ActiveSheet
Set GSheet = ActiveSheet
Set Tbl = d.Parent.Cells(d.Row, lCol).Name
' Define Data Range
LastRowC = GSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastColC = GSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set FRange = GSheet.Cells(1, 1).Resize(LastRowC, LastColC)
' Define Pivot Cache
Set FCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=FRange). _
CreatePivotTable(TableDestination:=FSheet.Cells(1, 12), _
TableName:=Tbl)
' Insert Blank Pivot Table
Set FTable = FCache.CreatePivotTable _
(TableDestination:=FSheet.Cells(1, 12), TableName:=Tbl)
With ActiveSheet.PivotTables(Tbl)
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
With FTable
For Each pvtFld In .PivotFields
pvtFld.Subtotals(1) = False
Next pvtFld
End With
' Insert Row Fields
With ActiveSheet.Name = ActiveSheet.PivotTables(Tbl).PivotFields("Check#")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.Name = ActiveSheet.PivotTables(Tbl).PivotFields("Object")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.Name = ActiveSheet.PivotTables(Tbl).PivotFields("Object Name")
.Orientation = xlRowField
.Position = 3
End With
' Insert Data Field
ActiveSheet.PivotTables(Tbl).AddDataField ActiveSheet.PivotTables( _
.Parent.Cells(d.Row, lCol)).PivotFields("Total"), "Sum of Total", xlSum
Next d
End With