PC Review


Reply
Thread Tools Rate Thread

Automating Excel to Create Pivot Table

 
 
=?Utf-8?B?UGV0ZQ==?=
Guest
Posts: n/a
 
      10th Feb 2005
I have written the following code to export a query from Access 2003 to Excel
2003 and then create a Pivot Table from it (I prefer Excel's implementation
of Pivot Tables to Access'). The part of the code that puts in the data field
and formats it works sometimes, but not others! I thought it was down to the
speed of my machine as sometimes it will work when you export for a second
time (even though Excel is closed on both occasions). I dont' get any error
messages and the rest of the code runs,i.e. it goes on to format the Pivot
Table. (I have created a reference to Excel 11 in Access). I can manually
drag the field into the Pivot Table afterwards without any problem.

'*** START OF CODE ***

DoCmd.OutputTo acOutputQuery, strReport, acFormatXLS, strReport &
".xls", True

Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
With xlApp
Dim intLastRow As Long, strRange As String
intLastRow = .ActiveSheet.UsedRange.Rows.Count
strRange = "qryStudyRevenueExport!R1C1:R" & intLastRow & "C6"
.ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:= _
strRange).CreatePivotTable TableDestination:="", _
TableName:="PivotTable1",
DefaultVersion:=xlPivotTableVersion10
.ActiveSheet.PivotTableWizard
TableDestination:=.ActiveSheet.Cells(3, 1)
.ActiveSheet.Cells(3, 1).Select
With .ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Customer/Supplier Name")
.Orientation = xlPageField
.Position = 1
End With
With .ActiveSheet.PivotTables("PivotTable1").PivotFields("Study
Code")
.Orientation = xlPageField
.Position = 1
End With
With
..ActiveSheet.PivotTables("PivotTable1").PivotFields("Department")
.Orientation = xlRowField
.Position = 1
End With
With .ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
.ActiveSheet.PivotTables("PivotTable1").AddDataField
ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Revenue (£)"), "Sum of Revenue (£)",
xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of
Revenue (£)")
.NumberFormat = "£#,##0"
End With
.ActiveWorkbook.ShowPivotTableFieldList = False
.ActiveSheet.PivotTables("PivotTable1").PivotSelect "",
xlDataAndLabel, True
.ActiveSheet.PivotTables("PivotTable1").Format xlTable1
End With
Set xlApp = Nothing

'*** END OF CODE ***
Any help appreciated
--
Peter Schmidt
Ross-on-Wye, UK
 
Reply With Quote
 
 
 
 
Andi Mayer
Guest
Posts: n/a
 
      10th Feb 2005
On Thu, 10 Feb 2005 08:19:04 -0800, "Pete"
<(E-Mail Removed)> wrote:

>I have written the following code to export a query from Access 2003 to Excel
>2003 and then create a Pivot Table from it (I prefer Excel's implementation
>of Pivot Tables to Access'). The part of the code that puts in the data field
>and formats it works sometimes, but not others! I thought it was down to the
>speed of my machine as sometimes it will work when you export for a second
>time (even though Excel is closed on both occasions). I dont' get any error
>messages and the rest of the code runs,i.e. it goes on to format the Pivot
>Table. (I have created a reference to Excel 11 in Access). I can manually
>drag the field into the Pivot Table afterwards without any problem.
>

try with a few DoEvents
---
If you expect an answer to a personal mail, add the word "manfred" to the first 10 lines in the message
MW
 
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
Using .Net to create Excel Pivot table Gary Dunne Microsoft VB .NET 3 22nd Feb 2010 07:23 PM
Excel 2007 - Create Pivot table from another pivot table =?Utf-8?B?VG9tIEJlcmdtYW4=?= Microsoft Excel Discussion 5 17th Nov 2007 12:51 AM
how to create pivot table from existing pivot table in excel 2007 =?Utf-8?B?VWRheXJhaiBEaHVsZWthcg==?= Microsoft Excel Misc 1 15th Dec 2006 04:23 AM
Automating an Access Pivot table export to Excel with VBA (2003) =?Utf-8?B?UFMgS2VsbGlnYW4=?= Microsoft Access VBA Modules 1 28th Oct 2005 06:45 PM
How do you create a pivot table in excel? bomba4vida Microsoft Access Reports 1 30th Sep 2005 02:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:36 PM.