PC Review


Reply
Thread Tools Rate Thread

Create a PivotTable from a VBA array - help!

 
 
KG Old Wolf
Guest
Posts: n/a
 
      19th Oct 2009
I want to run multiple pivot tables off an array exceeding 500k cells. I
believe it would vastly improve performance. I've tried variations of the
code below to no avail.

Set Pivot_Cache_01 = ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=Array_Work(1,1),(Rows_Count, Columns_Count))

I know there are: xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
xlScenario but I don't see which would accept an array as input.

Any ideas!





 
Reply With Quote
 
 
 
 
Patrick Molloy
Guest
Posts: n/a
 
      20th Oct 2009
I wasn't able to do this.

The only think that I could do was first to build a recortset object , then
add each field of my array into it. It worked ok ...however, I did need to
'fix ' the data

running TEst does this ...it simulates your array by loading data into
variant object. this is passed to a sub thats creates the pivot. To do this,
the BeCreative sub passes the data to a function taht returns a recoprdset,
which can easily be loaded to a pivot table.

I hope this inspires you. Let me know

Option Explicit

Sub test()
Dim data As Variant
data = Range("pivot.data")
BeCreative data
End Sub

Sub BeCreative(mydata As Variant)
Dim pc As PivotCache
Dim pt As PivotTable
Dim rst As Recordset
Set rst = GetRecordSet(mydata)

With ActiveWorkbook.PivotCaches.Add(xlExternal)
Set .Recordset = rst
.CreatePivotTable Range("C5"), "MyPivotdata"
End With
End Sub

Function GetRecordSet(data As Variant) As Recordset
Dim rs As Recordset
Dim index As Long, col As Long
Set rs = New Recordset
' 1st row is headers
With rs.Fields
For col = LBound(data, 2) To UBound(data, 2)
.Append data(1, col), adVarChar, 25
Next
End With
' add records skipping row 1
rs.Open
For index = LBound(data, 1) + 1 To UBound(data, 1)
rs.AddNew
For col = LBound(data, 2) To UBound(data, 2)
rs.Fields(col - 1) = data(index, col)
Next
Next

Set GetRecordSet = rs

End Function














"KG Old Wolf" wrote:

> I want to run multiple pivot tables off an array exceeding 500k cells. I
> believe it would vastly improve performance. I've tried variations of the
> code below to no avail.
>
> Set Pivot_Cache_01 = ActiveWorkbook.PivotCaches.Add _
> (SourceType:=xlDatabase, _
> SourceData:=Array_Work(1,1),(Rows_Count, Columns_Count))
>
> I know there are: xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
> xlScenario but I don't see which would accept an array as input.
>
> Any ideas!
>
>
>
>
>

 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      20th Oct 2009
PS My test data was 5244x7
and this routine worked pretty fast

"Patrick Molloy" wrote:

> I wasn't able to do this.
>
> The only think that I could do was first to build a recortset object , then
> add each field of my array into it. It worked ok ...however, I did need to
> 'fix ' the data
>
> running TEst does this ...it simulates your array by loading data into
> variant object. this is passed to a sub thats creates the pivot. To do this,
> the BeCreative sub passes the data to a function taht returns a recoprdset,
> which can easily be loaded to a pivot table.
>
> I hope this inspires you. Let me know
>
> Option Explicit
>
> Sub test()
> Dim data As Variant
> data = Range("pivot.data")
> BeCreative data
> End Sub
>
> Sub BeCreative(mydata As Variant)
> Dim pc As PivotCache
> Dim pt As PivotTable
> Dim rst As Recordset
> Set rst = GetRecordSet(mydata)
>
> With ActiveWorkbook.PivotCaches.Add(xlExternal)
> Set .Recordset = rst
> .CreatePivotTable Range("C5"), "MyPivotdata"
> End With
> End Sub
>
> Function GetRecordSet(data As Variant) As Recordset
> Dim rs As Recordset
> Dim index As Long, col As Long
> Set rs = New Recordset
> ' 1st row is headers
> With rs.Fields
> For col = LBound(data, 2) To UBound(data, 2)
> .Append data(1, col), adVarChar, 25
> Next
> End With
> ' add records skipping row 1
> rs.Open
> For index = LBound(data, 1) + 1 To UBound(data, 1)
> rs.AddNew
> For col = LBound(data, 2) To UBound(data, 2)
> rs.Fields(col - 1) = data(index, col)
> Next
> Next
>
> Set GetRecordSet = rs
>
> End Function
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> "KG Old Wolf" wrote:
>
> > I want to run multiple pivot tables off an array exceeding 500k cells. I
> > believe it would vastly improve performance. I've tried variations of the
> > code below to no avail.
> >
> > Set Pivot_Cache_01 = ActiveWorkbook.PivotCaches.Add _
> > (SourceType:=xlDatabase, _
> > SourceData:=Array_Work(1,1),(Rows_Count, Columns_Count))
> >
> > I know there are: xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
> > xlScenario but I don't see which would accept an array as input.
> >
> > Any ideas!
> >
> >
> >
> >
> >

 
Reply With Quote
 
KG Old Wolf
Guest
Posts: n/a
 
      20th Oct 2009
Hi Patrick -

While inspired, I recognize my limitations. I am an experienced programmer
learning VBA. To be honest, this is beyond my current skill set. I have
saved the solution to my "good ideas" folder for later use when I am move
familiar with the techniques you applied.

I need to be able to maintain this code so I have opted to do as much as I
can in the array work area and then output to "work ranges" in the format
best structured to the pivot tables I want to execute. Undoubtedly this will
have slower overall throughput but I can live with that until I can better
understand your solution.

INSPIRED - yes!

Thanks,
Ken

"Patrick Molloy" wrote:

> I wasn't able to do this.
>
> The only think that I could do was first to build a recortset object , then
> add each field of my array into it. It worked ok ...however, I did need to
> 'fix ' the data
>
> running TEst does this ...it simulates your array by loading data into
> variant object. this is passed to a sub thats creates the pivot. To do this,
> the BeCreative sub passes the data to a function taht returns a recoprdset,
> which can easily be loaded to a pivot table.
>
> I hope this inspires you. Let me know
>
> Option Explicit
>
> Sub test()
> Dim data As Variant
> data = Range("pivot.data")
> BeCreative data
> End Sub
>
> Sub BeCreative(mydata As Variant)
> Dim pc As PivotCache
> Dim pt As PivotTable
> Dim rst As Recordset
> Set rst = GetRecordSet(mydata)
>
> With ActiveWorkbook.PivotCaches.Add(xlExternal)
> Set .Recordset = rst
> .CreatePivotTable Range("C5"), "MyPivotdata"
> End With
> End Sub
>
> Function GetRecordSet(data As Variant) As Recordset
> Dim rs As Recordset
> Dim index As Long, col As Long
> Set rs = New Recordset
> ' 1st row is headers
> With rs.Fields
> For col = LBound(data, 2) To UBound(data, 2)
> .Append data(1, col), adVarChar, 25
> Next
> End With
> ' add records skipping row 1
> rs.Open
> For index = LBound(data, 1) + 1 To UBound(data, 1)
> rs.AddNew
> For col = LBound(data, 2) To UBound(data, 2)
> rs.Fields(col - 1) = data(index, col)
> Next
> Next
>
> Set GetRecordSet = rs
>
> End Function
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> "KG Old Wolf" wrote:
>
> > I want to run multiple pivot tables off an array exceeding 500k cells. I
> > believe it would vastly improve performance. I've tried variations of the
> > code below to no avail.
> >
> > Set Pivot_Cache_01 = ActiveWorkbook.PivotCaches.Add _
> > (SourceType:=xlDatabase, _
> > SourceData:=Array_Work(1,1),(Rows_Count, Columns_Count))
> >
> > I know there are: xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
> > xlScenario but I don't see which would accept an array as input.
> >
> > Any ideas!
> >
> >
> >
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a pivottable from a recordset? Rubble Microsoft Excel Programming 6 7th Oct 2008 05:14 AM
function to reference all of pivottable data field array =?Utf-8?B?YW5kcmV3?= Microsoft Excel Worksheet Functions 0 22nd Feb 2006 05:54 PM
How to create a Pivottable from Textfile =?Utf-8?B?RnJhbmsgTQ==?= Microsoft Excel Programming 0 23rd Sep 2005 11:07 AM
Create Excel PivotTable from VB.NET Jesus Carmona Microsoft VB .NET 0 20th Aug 2004 10:47 PM
Create a PivotTable from a VBA array? Dave Microsoft Excel Programming 0 9th Aug 2003 12:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:31 AM.