Pivot table vba failing after first success

TimW

Registered User.
Local time
Today, 09:41
Joined
Feb 6, 2007
Messages
90
Hi All
I wonder if anyone can shed light on my problem.
I am generating an Excel file from VBA on my database.
One of the requirements is to see the table results in a pivot table format. OK I thought - just use the pivot table automatically generated using VBA.
My problem is this code works the first time I run it & only the first time. If I shut the database down and restart it then it will run once.
Below is the bit of code that runs but does not produce a pivottable. Using the debugger the code steps through but does not actual create the pivottable. I took of the error handling but the code executes but does not produce the pivottable. Obviously something is happening but i am not sure what. I have tried setting open set items back to nothing but that is not helping.:banghead::banghead:
Any one any ideas please? :)
Code:
Dim pc As PivotCache
    Dim pt As PivotTable
    Dim pf1 As PivotField
 Dim rgPivot As Range

Code:
If boPivot Then
    '   Name the New Order sheet range for pivot table

    
        objXL.Names.Add Name:="NewOrders", RefersToR1C1:="=NewOrders!R1C1:R135C14"
        With xlWB 'workbook
            Set xlWS = .Worksheets.Add ' worksheet
            
' name worksheet and select starting point of pivot table
With xlWS
                .Name = "SOPivot"
                .Range("A3").Select
                
            
                
                Set pc = xlWB.PivotCaches.Create(xlDatabase, "=NewOrders")
                
                Set pt = pc.CreatePivotTable(Range("A3"), "NewOrderPivot")
                
                Set pf1 = pt.PivotFields("LoadDate")
                pf1.Orientation = xlColumnField
                pf1.Position = 1
                
                Set rgPivot = pf1.DataRange
                rgPivot.Cells(1).Group Start:=True, End:=True, Periods:=Array(False, False, False, False, True, False, True)
                

    
                Set pf1 = pt.PivotFields("Country")
                pf1.Orientation = xlRowField
                pf1.Position = 1
                
                Set pf1 = pt.PivotFields("Customer")
                pf1.Orientation = xlRowField
                pf1.Position = 2
                
                Set pf1 = pt.PivotFields("OrderValue")
                pf1.Orientation = xlDataField
                pf1.Function = xlSum
                pf1.Position = 1
                
                Set pf1 = pt.PivotFields("OrderQty")
                pf1.Orientation = xlDataField
                pf1.Function = xlSum
                pf1.Position = 2
                
                pt.DataPivotField.Orientation = xlRowField
                pt.DataPivotField.Position = 3
                
          
            End With
        End With
    End If

Set pf1 = Nothing
Set pt = Nothing
Set pc = Nothing
    
Set xlWS = Nothing
Set xlWB = Nothing

Set rgPivot = Nothing

I used the code from: http://www.cemtech.biz/using-vba-to-create-pivottable-and-pivotchart

Thanks inadvance
 

Users who are viewing this thread

Back
Top Bottom