0

I am trying to export data to excel and build a Pivot Table in the exported file using VBA. The below code runs exactly as intended the first time I run it upon opening Access. The second time I run it, I get a Run-time error '1004': Method 'Range of object '_Global' failed on line I marked below. The third time I run it, it works perfectly again.

I am at a loss as to why it would work and then not work. I added the wb.Close and xl.quit and set everything to Nothing before the sub ends so I would have thought it would be fine to run again.

Option Compare Database

Private Sub cmdRunManagerQuery_Click()
    Dim mySQL As String
    Dim Temp As Variant

    DoCmd.TransferSpreadsheet _
        acExport, _
        acSpreadsheetTypeExcel12Xml, _
        "qryManagerQuery", _
        "R:\Workpath\Manager Query.xlsx", _
        True

    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim lRowCount As Long
    Dim myRange As Excel.Range
    Set xl = CreateObject("Excel.Application")
    strInputFile = "R:\Workpath\Manager Query.xlsx"
    Set wb = xl.Workbooks.Open(strInputFile)
    Set ws = wb.Sheets("qryManagerQuery")

    'Test that I can edit the file
    'ws.Range("C250") = "=SUM(C2:C249)"

    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String
    Dim i As Integer
    Dim pf As String
    Dim pf_Name As String

    pf = "Number of Records"
    pf_Name = "Sum of Number of Records"
    i = 2
    Do While ws.Range("A" & i).Value <> ""
        i = i + 1
    Loop

    'ERROR OCCURS HERE...
    SrcData = ws.Name & "!" & Range("A1:D" & i - 1).Address(ReferenceStyle:=xlR1C1)

    Set sht = Sheets.Add
    StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=SrcData)

    Set pvt = pvtCache.CreatePivotTable( _
        TableDestination:=StartPvt, _
        TableName:="PivotTable1")

    pvt.PivotFields("1st Level Complete Date").Orientation = xlColumnField
    pvt.PivotFields("1st Level Analyst").Orientation = xlRowField
    pvt.AddDataField pvt.PivotFields("Number of Records"), pf_Name, xlSum

    wb.Save
    wb.Close
    xl.quit
    Set xl = Nothing
    Set wb = Nothing
    Set ws = Nothing
    Set sht = Nothing
    Set pvtCache = Nothing
    Set pvt = Nothing

    MsgBox "Export complete.  Files located at R:\Workpath", _
        vbInformation, _
        "Export Complete"

End Sub
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 3
    Note - your loop method of finding the last cell is not the best. See [this question](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba) for a better approach. – BigBen Nov 26 '19 at 14:31
  • You must not use unqualified Excel objects like `Range` or `ActiveWorkbook` in VBA running outside of Excel. Here is one of many duplicate targets: – Andre Nov 26 '19 at 16:19
  • 1
    Does this answer your question? [VBA Run-time error 1004: Method Range of object \_Global failed when trying to create tables in Excel 2013](https://stackoverflow.com/questions/37755435/vba-run-time-error-1004-method-range-of-object-global-failed-when-trying-to-cr) – Andre Nov 26 '19 at 16:19

0 Answers0