Link to home
Start Free TrialLog in
Avatar of Juan Velasquez
Juan VelasquezFlag for United States of America

asked on

Need help creating excel pivot table via vba

Hello,

I am trying to create a pivot table but am having problems with the create pivot table method.  I am creating it within Access via vba.  Below is the relevant code.  I can create the source data worksheet and used a debug.print rng.rows count to verify that the source data range contains data.

ws.cells.EntireColumn.AutoFit
    ws.Range("A2").Select
    objExcelApp.ActiveWindow.SplitRow = 1
    objExcelApp.ActiveWindow.SplitColumn = 0
    objExcelApp.ActiveWindow.FreezePanes = True
    
    'Set up pivot table
    Dim rng As Excel.Range
    Dim rngPiv As Excel.Range
    Dim sPT As Excel.PivotTable
    
    Set rng = ws.UsedRange
    Debug.Print rng.Rows.Count
    'Set up newest version pivot table
    wb.Sheets.Add
    Set wsPiv = wb.Sheets("Signal_Code")
    wsPiv.Activate
    
    Dim PTCache As Excel.PivotCache
    Dim PivotDataRange As Excel.Range

    Set PivotDataRange = rng
    Set PTCache = wb.PivotCaches.Create _
        (SourceType:=xlDatabase, _
        SourceData:=PivotDataRange, _
        Version:=xlPivotTableVersion12)
    Set sPT = PTCache.CreatePivotTable(TableDestination:=wsPiv.cells(4, 1), TableName:="SignalCode", DefaultVersion:=xlPivotTableVersion12)
    

     With sPT.PivotFields("Signal_Code")
        .Orientation = xlRowField
        .Position = 1
    End With
   sPT.AddDataField ActiveSheet.PivotTables("Total Value").PivotFields("Total Value"), "Sum of Total Value", xlSum
    'Change number VBA.Format
    With sPT.PivotFields("CountOfItemCode")
        .NumberFormat = "0"
    End With

Open in new window

Avatar of Juan Velasquez
Juan Velasquez
Flag of United States of America image

ASKER

Hello,

I found some mistakes and have corrected them but still cannot create the pivot table.

Dim rng As Excel.Range
    Dim rngPiv As Excel.Range
    Dim sPT As Excel.PivotTable
    
    Set rng = ws.UsedRange
    Debug.Print rng.Rows.Count
    'Set up newest version pivot table
    wb.Sheets.Add
    Set wsPiv = wb.Sheets("Signal_Code")
    wsPiv.Activate
    
    Dim PTCache As Excel.PivotCache
    Dim PivotDataRange As Excel.Range

    Set PivotDataRange = rng
    Set PTCache = wb.PivotCaches.Create _
        (SourceType:=xlDatabase, _
        SourceData:=PivotDataRange, _
        Version:=xlPivotTableVersion12)
    Set sPT = PTCache.CreatePivotTable(TableDestination:=wsPiv.cells(4, 1), TableName:="SignalCode", DefaultVersion:=xlPivotTableVersion12)
    

     With sPT.PivotFields("Signal_Code")
        .Orientation = xlRowField
        .Position = 1
    End With
   sPT.AddDataField ActiveSheet.PivotTables("SignalCode").PivotFields("Total Value"), "Sum of Total Value", xlSum
    'Change number VBA.Format
    With sPT.PivotFields("SumOfTotalValues")
        .NumberFormat = "0"
    End With

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Juan Velasquez
Juan Velasquez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeffrey Coachman
Great, ...you can accept your own post now...
BTW you also need to fix the unqualified use of ActiveSheet, or you'll find that Excel is left running.
I was able to figure out the problem on my own.