0

I am trying to create a pivot using below code, however I am receiving error,

"Invalid Procedure call or argument"

Can someone please help.

Here is my code:

Sub Creatingquerypivot()
    
        Dim sht As Worksheet
        Dim pvtCache As PivotCache
        Dim pvt As PivotTable
        Dim StartPvt As String
        Dim SrcData As String
        Dim Pf As PivotField

'Create Pivot Cache from Source Data
        Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=Sheets("Queries").Name & "!" & Sheets("Queries").Range("A3").CurrentRegion.Address)
                 
'Create a new worksheet
        Set sht = Sheets.Add
        sht.Name = "Queries Summary"

'Where do you want Pivot Table to start?
        StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlA1)
        
'Create Pivot table from Pivot Cache

        

 - Set pvt = pvtCache.CreatePivotTable( _
           TableDestination:=StartPvt, _
           TableName:="QueriesSummary")

    
    'To add fields to the pivot table
    
        Set Pf = pvt.PivotFields("Folder")
        Pf.Orientation = xlRowField
           
        Set Pf = pvt.PivotFields("Category")
        Pf.Orientation = xlRowField
            
        Set Pf = pvt.PivotFields("Received")
        Pf.Orientation = xlDataField
4
  • I think TableDestination expects a Range object, not a String. Commented Jul 21, 2020 at 13:41
  • Replacing String with Range is also not working. Commented Jul 21, 2020 at 13:51
  • "Not working" is not a helpful description. Can you edit your question with the revised code you are using, and the error message? Commented Jul 21, 2020 at 13:57
  • TableDestination expects a Variant - does not need to be a Range object, or recorded macros from creating a pivot table would not work - they always use the "range string" apporach. Commented Jul 21, 2020 at 17:18

2 Answers 2

1

As BigBen mentioned, TableDestination expects a Range object.

Dim StartPvt As Range

But you'll also need to Set it like so:

From:

StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlA1)

To:

Set StartPvt = sht.Range("A3")

Sign up to request clarification or add additional context in comments.

1 Comment

TableDestination expects a Variant, which can be eiother a string which evaluates to a Range, or an actual Range.
0

Worksheet names with spaces need to be single-quoted. It's good practice to always add quotes, even if the name has no spaces: it doesn't harm, and you don't have to worry about fixing your code if you change the sheet name later.

Edit: on testing, seems like it only works with R1C1 format range addresses?

Set sht = Sheets.Add
sht.Name = "Queries Summary"

StartPvt = "'" & sht.Name & "'!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)

Having said that, you already have Range objects you can use directly without first converting them to string range addresses:

    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=Sheets("Queries").Range("A1").CurrentRegion)
             
    Set sht = Sheets.Add
    sht.Name = "Queries Summary"
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range("A3"), _
                                        TableName:="QueriesSummary")

4 Comments

There is still an error here: StartPvt = "'" & sht.Name & "'!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1) Error is Runtime error '91' object variable or with block variable not set
How have you declared StartPvt, and is sht set to a worksheet ? I would drop the "range string" approach and just use the second method where you set the actual range object.
Yes, Dim StartPvt As Range and Dim sht As Worksheet are already there.
StartPvt is a String, not a Range

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.