Hi,
I am trying to create a pivot table on specific sheets.
Initially, I had hardcoded the name of my sheets in the below line
myarray = Array("ws1","ws2",etc)
But now that I have created a dynamic named range, it does not work anymore.
The problem returned at this point is a 'mismatched on the line
Set PTOutput = Sheets(pvtname)
Is there anybody who can tell me what I am not able to see to cope with that issue?
How can I improve the code I wrote below?
Thank you in advance for your help.
Note:
if 'MsgBox (myarray(i)) is not put as a comment, I also have a mismatch error on that line
Best,
Shiro
I am trying to create a pivot table on specific sheets.
Initially, I had hardcoded the name of my sheets in the below line
myarray = Array("ws1","ws2",etc)
But now that I have created a dynamic named range, it does not work anymore.
The problem returned at this point is a 'mismatched on the line
Set PTOutput = Sheets(pvtname)
Is there anybody who can tell me what I am not able to see to cope with that issue?
How can I improve the code I wrote below?
Thank you in advance for your help.
Note:
if 'MsgBox (myarray(i)) is not put as a comment, I also have a mismatch error on that line
Code:
Sub BuildPvt1020Loop_question()
Application.ScreenUpdating = False
Dim pt As PivotTable
Dim wst As Worksheet
Set wst = Worksheets("DataTable")
Dim PTCache As PivotCache
Dim PRange As Range
Dim PTOutput As Worksheet
Dim i As Integer
Dim myarray As Variant
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Range("myData"))
myarray = Array(Range("List1"))
For i = LBound(myarray) To UBound(myarray)
'MsgBox (myarray(i))
pvtname = (myarray(i))
Set PTOutput = Sheets(pvtname)
PTOutput.Range("20:100").EntireColumn.Delete
' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(10, 20), TableName:=pvtname)
pt.ManualUpdate = True
pt.AddFields RowFields:="RowLabels", ColumnFields:="ColLabels", PageFields:="ID"
With pt.PivotFields("DataValues")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
pt.PivotFields("ID").CurrentPage = (myarray(i))
pt.ManualUpdate = False
Next i
Erase myarray
Application.ScreenUpdating = True
End Sub
Best,
Shiro