Results 1 to 8 of 8
  1. #1
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398

    Creating a pivot table in Excel

    Although this code works, its leaving an instance of Excel open. What am I doing wrong?

    Dim xlApp As New Excel.Application


    Dim wb1 As Workbook

    Set xlApp = New Excel.Application
    Set wb1 = xlApp.Workbooks.Open("c:\chi\sla.xlsx")

    wb1.Sheets.Add
    wb1.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Qry1!R1C1:R2539C5", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14

    wb1.Sheets("Sheet1").Select
    wb1.ActiveSheet.Cells(3, 1).Select
    wb1.ActiveSheet.PivotTables("PivotTable1").AddData Field ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Tran_Amount"), "Sum of Tran_Amount", xlSum

    xlApp.ActiveWorkbook.Close (True)


    Set wb1 = Nothing
    Set xlApp = Nothing

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    did you try wb1.close as well since that's the actual object that has the open file?

  3. #3
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    No, I haven't.

    It has something to do with "wb1.ActiveSheet.PivotTables..." statement. If I comment out that line, The code works properly.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I'd still try the wb1.close and see if that works and possibly issue a refresh command before you try to close it.
    I don't know if it will help you either but I've run into some funky things with excel and you may need to save the file then issue the close command as well. I'm not hugely versed in excel functions because I just don't use them that often but I do remember running into a situation similar to this and it was tied to not saving the file or not issuing that workbook.close statement, I honestly don't remember which it was.



  5. #5
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    Thanks for your suggestions. I'll give it a try and see what happens.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I have run into this as well. Some manipulations of spreadsheet cause the Excel instance to persist. Even the Close method won't clear it. Don't remember if running Save method first helps.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    Here's what I got to work:

    Dim xlApp As New Excel.Application
    Dim wb1 As Workbook

    Set xlApp = New Excel.Application
    Set wb1 = xlApp.Workbooks.Open("c:\chi\sla.xlsx")

    wb1.Sheets.Add
    wb1.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Qry1!R1C1:R2539C5", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14

    wb1.Sheets(1).Select
    wb1.Sheets(1).Cells(3, 1).Select
    wb1.Sheets(1).PivotTables("PivotTable1").AddData Field wb1.Sheets(1).PivotTables( _
    "PivotTable1").PivotFields("Tran_Amount"), "Sum of Tran_Amount", xlSum

    xlApp.ActiveWorkbook.Close (True)

    xlApp.Quit

    Set wb1 = Nothing
    Set xlApp = Nothing


    Thanks for your assistance!!

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Check Task Manager. Make sure the Excel instance is truly terminated.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Creating a pivot table
    By crowegreg in forum Reports
    Replies: 1
    Last Post: 01-08-2014, 07:29 PM
  2. Access 2010 VBA Creating Pivot Table in Excel
    By Tomlon in forum Programming
    Replies: 1
    Last Post: 02-07-2013, 02:25 PM
  3. Creating a pivot table like query
    By razkowski in forum Queries
    Replies: 2
    Last Post: 07-03-2012, 09:45 AM
  4. Creating Excel pivot table issue
    By Reaper in forum Import/Export Data
    Replies: 1
    Last Post: 06-14-2012, 12:32 PM
  5. Pivot table - access to excel
    By antagonia in forum Access
    Replies: 3
    Last Post: 12-16-2011, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums