Excel 2007 Pivot Table creation through VBA

luanub

New Member
Joined
Nov 19, 2013
Messages
15
I am trying to create a pivot table through a VBA function. I'm using Excel 2007. I have tried several different methods and can not get Excel to create the table. Unfortunately I am not getting any error messages and the code runs fine. Its just after it is done there is no table, the sheet that I am trying to build it on remains blank.

Here are a couple of the functions that I have tried, I have also tried the wizard and recording the function as a macro and neither seems to work. Any idea's on what is going wrong?

1st code
Code:
Function createPivot()
    Dim WSD As Worksheet
    Dim wsData As Worksheet
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range
    Dim FinalRow As Long
    Dim FinalCol As Long
    Set WSD = Worksheets("Hourly Report")
    Set wsData = Worksheets("Department_Summary")

'Delete any prior pivot tables
    For Each PT In WSD.PivotTables
        PT.TableRange2.Clear
    Next PT

    Set PRange = wsData.Range("A2:R200")

    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)

    Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Range("A1"), TableName:="PivotTable1")

    PT.ManualUpdate = True

    PT.AddFields RowFields:="Rack Type", ColumnFields:="Op Seq"

    With PT.PivotFields("Queue Qty")
        .Orientation = xlDataField
        .Function = xlSum
        .Position = 1
    End With

'Calc the pivot table
    PT.ManualUpdate = False
    PT.ManualUpdate = True
End Function

2nd Code
Code:
    Dim wsData As Worksheet
    Dim wsPvtTbl As Worksheet
    Dim rngData As Range
    
    Set wsData = Worksheets("Department_Summary")
    Set wsPvtTbl = Worksheets("Hourly Report")
    Set rngData = wsData.Range("A2:R200")
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, _
        Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=wsPvtTbl.Range("A1"), TableName:= _
        "myChart", DefaultVersion:=xlPivotTableVersion12
    
    With ActiveSheet.PivotTables("myChart").PivotFields("Op Seq")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("myChart").PivotFields("Rack Type")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("myChart").AddDataField ActiveSheet.PivotTables( _
        "myChart").PivotFields("Queue Qty"), "Sum of Queue", xlSum

    ActiveSheet.PivotTables("myChart").PivotFields("Queue Qty").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("myChart").PivotFields("Rack Type").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    ActiveSheet.PivotTables("myChart").PivotFields("Op Seq").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    
    With ActiveSheet.PivotTables("myChart")
        .ColumnGrand = False
        .RowGrand = False
    End With
    ActiveSheet.PivotTables("myChart").RowAxisLayout xlOutlineRow

Ultimately I would like to get all the options in the 2nd code to work but at this point will settle for anything.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
How are you calling the function?

The function is being called from an ActiveX button.
Code:
Private Sub CommandButton1_Click()
    createPivot
End Sub

I have also tried just entering the code as a macro and running it that way. I get the same results either way.
 
Upvote 0
Your code worked fine for me in Excel 2007. Do you have On Error Resume Next anywhere in your code? Are there headings in row 2 on worksheet "Department_Summary"?
 
Upvote 0
Your code worked fine for me in Excel 2007. Do you have On Error Resume Next anywhere in your code? Are there headings in row 2 on worksheet "Department_Summary"?

Yes there is On Error Resume Next, and yes there are headings on row 2.

I'm guessing this is due to the On Error? I will go in and try to remove what I can and see if I can get it to work.

Thank you for your help, it's very much appreciated.


EDIT:

So that definately changed something. Now using either set of code I get a RunTime Error 13 Type Mismatch on the following lines.

Code:
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)

Code:
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, _
        Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=wsPvtTbl.Range("A1"), TableName:= _
        "myChart", DefaultVersion:=xlPivotTableVersion12
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,377
Members
448,955
Latest member
BatCoder

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