mismatch error, pivot build automation, multisheets specific

Shiro26

Board Regular
Joined
Oct 2, 2015
Messages
82
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


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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Might be easier if you don't use arrays, eg

Code:
For Each c In Range("List1")
    MsgBox c.Value
    pvtname = c.Value
    
    'rest of code here
    
Next
 
Upvote 0
If you did want to use arrays, then here's an example

Code:
Dim myarray() As Variant
myarray = Range("List1")
MsgBox myarray(3, 1)

If List1 was Range("A1:A10"), then that would list the value in A3.
Assigning a range to an array creates a 2 dimensional array.
 
Upvote 0
If you did want to use arrays, then here's an example

Code:
Dim myarray() As Variant
myarray = Range("List1")
MsgBox myarray(3, 1)

If List1 was Range("A1:A10"), then that would list the value in A3.
Assigning a range to an array creates a 2 dimensional array.

Hello,

Thank you for your input. This makes sens.
I am going to try and will post again if I face another issue.
Thank you again

Best,

Shiro
 
Upvote 0
Hi,

So as you recommended I have changed the code to

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
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Range("myData"))
    For each c in Range("List1")
        pvtname = c.value
        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 = c.value
            pt.ManualUpdate = False
    Next c
        
Application.ScreenUpdating = True
End Sub

However, now I have an error message of overflow on the line Set PTOutput = Worksheets(pvtname)
Do you have any idea why is that so by any chance?

Should I dim the 'pvtname' and the 'c' as well?


Kind Regards,

Shiro
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top