Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Create a PivotTable from a VBA array - help!

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 a xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
xlScenario but I don't see which would accept an array as input.

Any ideas!





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Create a PivotTable from a VBA array - help!

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 a xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
xlScenario but I don't see which would accept an array as input.

Any ideas!





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Create a PivotTable from a VBA array - help!

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 a xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
xlScenario but I don't see which would accept an array as input.

Any ideas!





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Create a PivotTable from a VBA array - help!

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 a xlConsolidation, xlDatabase, xlExternal, xlPivotTable, and
xlScenario but I don't see which would accept an array as input.

Any ideas!





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a pivottable from a recordset? Rubble Excel Programming 6 October 7th 08 05:14 AM
Create a Pivottable in a macro Michael Excel Discussion (Misc queries) 8 March 15th 07 02:41 PM
function to reference all of pivottable data field array andrew Excel Worksheet Functions 0 February 22nd 06 05:54 PM
How to create a Pivottable from Textfile Frank M Excel Programming 0 September 23rd 05 11:07 AM
Create a PivotTable from a VBA array? Dave[_15_] Excel Programming 0 August 9th 03 12:58 PM


All times are GMT +1. The time now is 04:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"