1

I am trying to add a pivot table to the same worksheet I am on (the sheet is called holders (corp)) but am having trouble with that.

Sub PivotTable()

Sheets("Sheet2").Select

Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim pf As PivotField
Dim StartPvt As String
Dim SrcData As String
Dim LastRow As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).row

'Determine the data range you want to pivot
 SrcData = ActiveSheet.Name & "!" & Range(Cells(1, "A"), Cells(LastRow,"E")).Address(ReferenceStyle:=xlR1C1)

'Create a new worksheet
 Set sht = Sheets("HOLDERS (CORP)")

'Where do you want Pivot Table to start?

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

'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)

'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub

I get a debug issue related to the call procedure at the very last 3 lines of code but am not sure why. Help would be much appreciated!

2
  • It would help if you posted the error message ;) Commented Jul 19, 2016 at 18:27
  • I receive a message saying invalid call procedure or something of that sort! I'll post it Commented Jul 19, 2016 at 18:41

1 Answer 1

1

Excel VBA cannot accept R1C1 cell addresses in Range objects.

Change

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

To

StartPvt = sht.Range("A1").Address

Then change

Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub

To

Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), _
    TableName:="HolderssPivotTable")

Update - Full refactored code to ensure pivot table ends up on Holders (CORP) sheet.

Sub PivotTable()

Dim sht2 As Worksheet
Set sht2 = Sheets("Sheet2")

With sh2

    Dim LastRow As Long
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    'Determine the data range you want to pivot
    Dim SrcData As String
    SrcData = .Name & "!" & .Range(.Cells(1, "A"), .Cells(LastRow, "E")).Address

End With

Dim sht As Worksheet
'Create a new worksheet
Set sht = Sheets("HOLDERS (CORP)")

'Where do you want Pivot Table to start?
Dim StartPvt As String
StartPvt = sht.Range("A1").Address

'Create Pivot Cache from Source Data
Dim pvtCache As PivotCache
Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData)

'Create Pivot table from Pivot Cache
Dim pvt As PivotTable
Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), TableName:="HolderssPivotTable")

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

5 Comments

Thanks Scott, but this method still adds the pivot table on a new sheet, is there a way for me to make it add it to the sheet named HOLDERS(CORP)?
@markos - it worked for me when I tested, but I refactored the code to make it cleaner by assigning all ranges and objects and working directly with them.
seems to be working! Thanks a million! How come it now moves it to HOLDER(CORP)? It seems like it still adds a new sheet based on the code. But based on when I run it, a new sheet isn't added but the table is added on my sheet named HOLDERS (CORP)
@markos - this line Set sht = Sheets("HOLDERS (CORP)") does not create a new sheet (i just left your comment there). It sets the variable sht to the existing sheet. Please mark as answered so others benefit.
@Scott Holtzman Typo ~Set sht2 = Sheets("Sheet2") With sh2~ to With sht2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.