Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

PivotTable.Add, Error

Hello - I have a VB6 application in which I retrieve data from my DB; open excel and create 1 workbook with several worksheets; data goes on one sheet and charts to be created on another; I then create my pivotcache and pivottable; pivot tables are then used to create charts. Initial run is great...2nd and future runs I get an error (Application-defined or object-defined error) on my pivot table add unless I close down the whole app. I set all to nothing when done and do not save any work book or xls document, leaving it to the user. Code follows for the section with error:

Dim lTable As Excel.PivotTable
Dim lCache As Excel.PivotCache

Set lCache = lWorkbook.PivotCaches.Add(xlDatabase, _
lDataSheet.Range("A1:" & lLastCell))
Set lTable = lDataSheet.PivotTables.Add(lCache, Range("H1"))

I have tried several different ways of creating pivot tables, including pivot table wizard - but all produce an error when creating pivot table on second and future runs. Have searched for similar problems and found one really close but even that resolution did not help on 2nd and future runs. Has anyone run across this issue before? Any info would be greatly appreciated. Can provide code and any further information
[1252 byte] By [chilekitty] at [2007-11-11 7:18:18]
# 1 Re: PivotTable.Add, Error
Issue resolved......I was able to get it to run many times without errors only when I changed all references to their actual object instead of using activesheet, activechart, etc..

Like This:
Set lTable = lDataSheet.PivotTables.Add(lCache, lDataSheet.Range("H1"))
Instead of:
lDataSheet.Activate
Set lTable = lDataSheet.PivotTables.Add(lCache, Range("H1"))
chilekitty at 2007-11-11 17:27:12 >