I keep running into this same issue and it's driving me nuts. I have an Excel workbook with a Pivot Table in it.
Basically all I want to do is push data from an MS Access query into an Excel worksheet and then refresh the pivot table. I need to do this all from an MS Access form. With a button OnClick event I first open the workbook and delete the worksheet used by a pivot table on another worksheet (i.e. I delete "Sheet1"), then close the workbook... using: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml... I post the new data (i.e. new "Sheet1"). I then open the workbook and refresh the pivot... pretty straightforward, but I keep running into this issue:
The code works fine on the first run, then breaks when I try to run it again, leaving the Excel spreadsheet open... Then when I end the event and close the spreadsheet manually, the button click will work again just fine, running the code and closing the workbook... then I try again and it breaks with the workbook open... over and over.
Also, see screen shots of the error and where it breaks
Can someone please tell me why my code is breaking, every other run?
Basically all I want to do is push data from an MS Access query into an Excel worksheet and then refresh the pivot table. I need to do this all from an MS Access form. With a button OnClick event I first open the workbook and delete the worksheet used by a pivot table on another worksheet (i.e. I delete "Sheet1"), then close the workbook... using: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml... I post the new data (i.e. new "Sheet1"). I then open the workbook and refresh the pivot... pretty straightforward, but I keep running into this issue:
The code works fine on the first run, then breaks when I try to run it again, leaving the Excel spreadsheet open... Then when I end the event and close the spreadsheet manually, the button click will work again just fine, running the code and closing the workbook... then I try again and it breaks with the workbook open... over and over.
Code:
Private Sub btnManipulateFiles_Click()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim outputFileName As String
outputFileName = CreateObject("WScript.Shell").specialfolders("Desktop") & "\EXWC_COMMITMENTS_AND_OBLIGATIONS.xlsx"
Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Application.DisplayAlerts = False
Set xlWB = xlApp.Workbooks.Open(outputFileName)
Set xlSh = xlWB.Sheets("Sheet1")
xlSh.Activate
'Delete sheet to make room for new
xlSh.Delete
xlApp.Application.DisplayAlerts = True
xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
'Output 00100_OPN_COMMITMENT_AND_OBLIGATIONS into the specified Workbook as Sheet1.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "00100_OPN_COMMITMENT_AND_OBLIGATIONS", outputFileName, True, "Sheet1"
Set xlApp = New Excel.Application
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Open(outputFileName)
'Refresh Pivot Table
Set xlSh = xlWB.Sheets("Obs and Commits by LIRN")
xlSh.Activate
Sheets("Obs and Commits by LIRN").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "SBH[All]", xlLabelOnly, _
True
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Sheet1", Version _
:=xlPivotTableVersion14)
xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
End Sub
Also, see screen shots of the error and where it breaks
Can someone please tell me why my code is breaking, every other run?