add data to pivot table cache

obiron

Active Member
Joined
Jan 22, 2010
Messages
469
Hi guys,

I am stuck with using Excel 2003 via Citrix and do not have Access, ODBC connections or Microsoft query installed :(

I need to build a pivot table which could potentially run to 1M rows, but seem to be limited by the fact that a pivot table must have a source and my only source is a worksheet which is obviously limited to 65500 records. Is there any way to add records to the cache using VBA. I would obviously lose the ability to refresh the table but that is a price I am willing to pay.

If not, is there another way to collate multiple sheets together bearing in mind I cannot access any external data sources. I can't even read data from a text file. Selecting multiple ranges doesn't so what I need.

TIA

Obiron
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
The multiple consolidation ranges that get posted here alot tend to rely on unioing the data and pulling together a recordset using ADO. I don't think that method will work for you because the data needs to be placed in a separate workbook due to an issue where a memory leak occurs when querying an open workbook.

So, in light of this all I can think of is building the recordset in memory. Something like this:

Code:
Public Sub ConsolidateAndPivot()
    Dim rngCell As Range, strRange As String, rngData As Range
    Dim pvc As PivotCache, pvt As PivotTable
    Dim arrSheets As Variant, wks As Worksheet
    Dim recData As ADOR.Recordset
    
    Set recData = New ADOR.Recordset
    arrSheets = Array("Sheet1", "Sheet2", "Sheet3")
    strRange = "A:E"
    Set pvt = Sheets(1).PivotTables(1)
    
    For Each wks In Sheets(arrSheets)
        Set rngData = Intersect(wks.Range(strRange), wks.UsedRange)
        If wks.Name = arrSheets(0) Then
            For Each rngCell In rngData.Resize(1)
                With recData
                    With .Fields
                        Select Case TypeName(rngCell.Offset(1).Value)
                            Case "String"
                                .Append rngCell.Text, adVarChar, 255
                            Case "Date"
                                .Append rngCell.Text, adDate
                            Case "Double", "Currency"
                                .Append rngCell.Text, adDouble
                        End Select
                    End With
                End With
            Next rngCell
        End If
        If recData.State = 0 Then recData.Open
        Set rngData = Intersect(rngData.Offset(1), wks.UsedRange)
        For Each rngCell In rngData
            With recData
                If rngData(0, rngCell.Column).Text = .Fields(0).Name Then .AddNew
                .Fields(rngData(0, rngCell.Column).Text).Value = rngCell.Value
                .Update
            End With
        Next rngCell
    Next wks
    
    With Sheets.Add(Sheets(1))
        Set pvc = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
        Set pvc.Recordset = recData
        With pvc.CreatePivotTable(TableDestination:=.Range("A1"))
            pvt.CacheIndex = .CacheIndex
        End With
        Application.DisplayAlerts = False
            .Delete
        Application.DisplayAlerts = True
    End With
    
    recData.Close
    Set recData = Nothing
End Sub

This builds a pivot cache from data in Sheet1, Sheet2 and Sheet3, with the data in columns A:E. Consistent structure across sheets is expected (i.e. same field names and in same order). It updates a pivot table in the first sheet in the workbook.
 
Upvote 0
I will add, that for large recordsets, this will take forever and an age to build...
 
Upvote 0
Thanks for the script. Unfortunately it falls over at line 4
Code:
Dim recData As ADOR.Recordset

with error message Compile Error: user defined type not defined.

I wouldn't mind all this mucking about if it wasn't for the fact that the originating data is in Oracle BI but the IS department won't allow ODBC access to the database - go figure.... How can you analyse data when you are not allowed to access the data?
 
Upvote 0
Ah yes, I should have added, you require a reference:

In the VBE go Tools > References

Find Microsoft ActiveX Data Objects Recordset 2.x Library

I am using 2.8...


I feel your pain buddy! :)
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

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