VB6中logging集的透视表

我有一个数据透视表的Excel文件,我正在寻找与logging集中的数据使用。 到目前为止,这是我所拥有的

Dim xlApp As Excel.Application Dim xlWbook As Excel.Workbook Dim xlWSheet As Excel.Worksheet Dim xlptCache As Excel.PivotCache Dim xlptTable As Excel.PivotTable Dim pivotRecordSet As ADODB.Recordset 'Open Excel File and set data for pivotRecordSet With xlWbook Set xlWSheet = .Worksheets("Sheet1") Set xlptCache = .PivotCaches.Add(SourceType:=xlExternal) 'Trying this gives me an Application-defined or object-defined error 'Set .PivotCaches.item(0).Recordset = pivotRecordSet End With 'I also tried this with the same error when setting the recordset Set xlptTable = xlWSheet.PivotTables("PivotTable1") Set xlptTable.PivotCache.Recordset = pivotRecordSet 

我知道我可以用这个创build一个新的数据透视表

  Set xlptTable = xlWSheet.PivotTables.Add(PivotCache:=xlptCache, TableDestination:=xlWSheet.Range("D4"),tablename:="PT_Report") 

有什么办法可以改变,使用现有的数据透视表,而不是创build一个新的? 或者我做错了更改导致错误的logging集?

要更正您所评论的错误,请尝试以下操作:

 Set xlptCache.Recordset = pivotRecordSet 

而不是像你那样:

 Set xlptTable = xlWSheet.PivotTables.Add(xlptCache, Range("D4"), "PT_Report") 

我想如果你发布更多的代码,你会有更多的运气。 下面有一个适用于我的示例(基于具有相同结构数据的现有数据透视表)。

有许多事情会导致“应用程序定义的错误或对象定义的错误”。

我发现两个是:

  • 不打开logging集
  • 没有find数据透视表 – 例如在我的代码中,我通过ActiveSheet引用数据透视表 – 如果我在Excel中select一个没有数据透视表的工作表,我会得到这个错误。

看看你的代码,有几件事情,我觉得有问题,你可能要检查:

  • Set xlptCache = .PivotCaches.Add(SourceType:=xlExternal) – 为什么要添加一个PivotCache? 您需要更新属于您的现有数据透视表的PivotCache。
  • Set .PivotCaches.item(0).Recordset = pivotRecordSet – 我也不喜欢这个,因为你没有引用特定的PivotCache – 只是表单中的第一个。

我认为你应该通过名称引用PivotTable PivotCache ,然后访问属于该PivotTable PivotCache (见下文)。

我build议你做的就是激活你的debugging器,并逐步完成,并确保你已经打开了logging集,然后再find它,确保你已经提到了正确的数据透视表,获得一个PivotCache,并检查正确的对象被卡在正确的地方。

下面的代码在我的机器上工作(使用Excel 2010)。

请注意:我使用ActiveWorkbook.ActiveSheet – 来说明获取错误的方法之一。 使用.Worksheets("Sheet1")机制对于实际代码是一个更好的主意。

 Sub changePivot() Dim cnnConn As ADODB.Connection Dim rstRecordset As ADODB.Recordset Dim cmdCommand As ADODB.Command ' Open the connection. Set cnnConn = New ADODB.Connection With cnnConn .ConnectionString = _ "Provider=Microsoft.Jet.OLEDB.4.0" .Open "C:\Users\gregh\Documents\Database1.mdb" End With ' Set the command text. Set cmdCommand = New ADODB.Command Set cmdCommand.ActiveConnection = cnnConn With cmdCommand .CommandText = "Select Speed, Pressure, Time From DynoRun2" .CommandType = adCmdText .Execute End With ' Open the recordset. Set rstRecordset = New ADODB.Recordset Set rstRecordset.ActiveConnection = cnnConn ' if you don't do this, you get the error rstRecordset.Open cmdCommand Dim sh As Excel.Worksheet Dim pt As Excel.PivotTable Dim pc As Excel.PivotCache ' Get a hold on the pivot table then the cache ' I can trigger the error you mentioned if the ActiveSheet doesn't have the pivot table ' Your mechanism of referring to the sheet by name is a much better idea. Set sh = ActiveWorkbook.ActiveSheet Set pt = sh.PivotTables("Performance") Set pc = pt.PivotCache Set pc.Recordset = rstRecordset ' The PivotTable doesn't update until you call this pc.Refresh ' Close the connections and clean up. cnnConn.Close Set cmdCommand = Nothing Set rstRecordset = Nothing Set cnnConn = Nothing End Sub