reymon2012
New Member
- Joined
- Feb 3, 2012
- Messages
- 17
Still learning VBA and code below is for creating Pivot Table. However i'm getting Subscript out of Range Error for line item 6. Highlighted in yellow below.
i'm pulling the report from database and the source data worksheet name is " Prompted Tasks Grid" and the destination worksheet name is " Pivot Table".
Any help is sincerely appreciated
i'm pulling the report from database and the source data worksheet name is " Prompted Tasks Grid" and the destination worksheet name is " Pivot Table".
Any help is sincerely appreciated
VBA Code:
Sub Create_Pivot_Table()
Dim LastRow As Long, LastColumn As Long
Dim DataRange As Range
Dim PTCache As PivotCache
Dim PT As PivotTable
Set wb = ThisWorkbook
[COLOR=rgb(250, 197, 28)][B]Set wsData = ThisWorkbook.Worksheets("Prompted Tasks Grid")[/B][/COLOR]
With wsData
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
Set DataRange = .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
Set wsPT = wb.Worksheets.Add
wsPT.Name = "Pivot Table"
Set PTCache = wb.PivotCaches.Create(xlDatabase, DataRange)
Set PT = PTCache.CreatePivotTable(wsPT.Range("B5"), "Pt_CADBTask")
With PT
'//Pivot Table Layout Settings
.RowAxisLayout xlTabularRow
.ColumnGrand = True
.RowGrand = False
.TableStyle2 = "PivotStyleMedium9"
.HasAutoFormat = False
.SubtotalLocation xlAtBottom
'//Row Section (layer1)
With .PivotFields("Assigned To Name")
.Orientation = xlRowField
.Position = 1
.LayoutBlankLine = False
.Subtotals(1) = True
.LayoutForm = xlTabular 'xloutline
.LayoutCompactRow = True
End With
'// Values Section
With .PivotFields("Loan #")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##;(#,##);-"
.Caption = "Quanty"
End With
wsPT.Cells.EntireColumn.AutoFit
End With
End With
End Sub