user2021539
user2021539

Reputation: 967

PivotTable error 1004

Can anyone see why this code would cause a 1004 error on the last line? Everything works great up until that last line. I had it working, then it started getting this error and I can't figure out why. Sheet2 is a blank sheet. Sheet1 is currently just test data, 10 rows, 3 columns. It starts at B3. Anyone have any ideas?

    Sub CreatePivot()
        ' Define RngTarget and RngSource as Range type variables
        Dim RngTarget As Range
        Dim RngSource As Range
        Dim intLastCol As Integer
        Dim intCntrCol As Integer

        ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
        Set RngTarget = ThisWorkbook.Worksheets("Sheet2").Range("B3")

        ' RngSource defines the Range that will be used to create the PivotTable
        ' ActiveWorkbook = The currently opened Workbook
        ' ActiveSheet = The currectly opened sheet
        ' UsedRange = The Range of cells with active data in them
        Set RngSource = ActiveWorkbook.ActiveSheet.UsedRange

        ' Select the Range
        RngSource.Select

        ' Copy the Range into the clipboard
        RngSource.Copy

        ' Create a new PivotTable using the RngSource defined above,
        ' in Excel format,
        ' placed at the RngTarget location,
        ' And name it PivotB3 just for reference if needed
        ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource).CreatePivotTable RngTarget, "PivotB3"

        ' Get the last used column from the data table
        intLastCol = RngSource.Columns(RngSource.Columns.Count).Column

        ' Select the Pivot table so we can apply the conditional formats
        ActiveSheet.PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True

Upvotes: 2

Views: 4017

Answers (2)

Jon Crowell
Jon Crowell

Reputation: 22358

You are getting the error because the pivot table is on sheet2 and not the activesheet. You could fix the error by simply selecting sheet2 prior to selecting the pivot table, but what you really want to do is eliminate all selects from your code. See Avoid Using Select for further explanation/examples.

Try this:

Sub CreatePivot()
        Dim RngTarget As Range
        Dim RngSource As Range
        Dim ws As Worksheet
        Dim pt As PivotTable

        Set ws = ThisWorkbook.Sheets("Sheet2")
        ws.Cells.Clear
        ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
        Set RngTarget = ws.Range("B3")

        Set RngSource = ActiveWorkbook.ActiveSheet.UsedRange

        ' Create a new PivotTable
        ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource).CreatePivotTable _
            RngTarget, "PivotB3"
        Set pt = RngTarget.PivotTable

        ' We now have access to the pivot table and can modify as needed
        pt.PivotSelect "", xlDataAndLabel, True
        'ActiveSheet.PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True
End Sub

NOTE: I've removed variables and comments that aren't part of your issue to make it easier to see what is happening.

Upvotes: 1

Santosh
Santosh

Reputation: 12353

Try below code :

Sub CreatePivot()
' Define RngTarget and RngSource as Range type variables
    Dim RngTarget As Range
    Dim RngSource As Range
    Dim intLastCol As Integer
    Dim intCntrCol As Integer

    ' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
    Set RngTarget = ThisWorkbook.Worksheets("Sheet2").Range("B3")

    ' RngSource defines the Range that will be used to create the PivotTable
    ' ActiveWorkbook = The currently opened Workbook
    ' ActiveSheet = The currectly opened sheet
    ' UsedRange = The Range of cells with active data in them
    Set RngSource = ActiveSheet.UsedRange

    ' Select the Range
   ' RngSource.Select

    ' Copy the Range into the clipboard
   ' RngSource.Copy

    ' Create a new PivotTable using the RngSource defined above,
    ' in Excel format,
    ' placed at the RngTarget location,
    ' And name it PivotB3 just for reference if needed
    Dim oPC As PivotCache
   Set oPC = ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource)

   Dim oPT As PivotTable
   Set oPT = oPC.CreatePivotTable(RngTarget, "PivotB3", True)

    ' Get the last used column from the data table
    intLastCol = RngSource.Columns(RngSource.Columns.Count).Column

    ' Select the Pivot table so we can apply the conditional formats
    'Worksheets("Sheet2").PivotTables("PivotB3").PivotSelect "", xlDataAndLabel, True
End Sub

Upvotes: 0

Related Questions