3

I'm using the following code:

Sub MakeAPivotTable()

    Dim pt As PivotTable
    Dim cacheOfpt1 As PivotCache 'This is the Source Data
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim LastCol As Long
    Dim LastRow As Long
    Dim PRange As Range


    LastCol = Sheets("Sheet2").Range("A1").SpecialCells(xlCellTypeLastCell).Column
    LastRow = Sheets("Sheet2").Range("A1").SpecialCells(xlCellTypeLastCell).Row
    Set PRange1 = Sheets("Sheet2").Cells(1, 1).Resize(LastRow, LastCol)

    On Error GoTo err_add_Y_next_row

    Sheets("Sheet3").Select
    ActiveSheet.PivotTables("PivotTable1").TableRange2.Clear  'Delete any Pivot Table

    'set the cache of PT
    Sheets("Sheet2").Select
    Set cacheOfpt1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, PRange1)

    'create the pt
    Sheets("Sheet3").Select
    Set pt = ActiveSheet.PivotTables.Add(cacheOfpt1, Range("a1"), "PivotTable1")


    'put the Fields in

    With pt
    'add the Fields
    .PivotFields("CAMPAIGN").Orientation = xlRowField
    '.PivotFields("TAG1").Orientation = xlRowField
    '.PivotFields("TAG2").Orientation = xlRowField
    '.PivotFields("TAG3").Orientation = xlRowField
    .PivotFields("CIRN").Orientation = xlColumnField
    .PivotFields("RUN").Orientation = xlPageField  'Column Filter
    .PivotFields("TVSN").Orientation = xlDataField  'Value Field

    'set the number format
    .DataBodyRange.NumberFormat = "#,##0.00"


    'go to classic View
    .RowAxisLayout xlTabularRow
    End With

err_add_Y_next_row:
MsgBox Err.Number & Err.Description, _
    vbExclamation, "VBAtools.pl"

End Sub

But when I'm running the code, I'm getting the error "The PivotTable field name is not valid. To create a pivot table report you must use data that is organized as a list with labeled columns, If you are changing the name of a PivotTable field, you must type a new name for the field."

This code was running fine. But suddenly I started getting this error.

Can anybody please help me find the reason.

Thank you.

7
  • Did you check the PivotTable name? :-) Commented Sep 2, 2013 at 15:49
  • What line does the error occur on? Commented Sep 2, 2013 at 15:51
  • @Makah: Yes I checked the PivotTable Name. Commented Sep 2, 2013 at 15:55
  • @Doug: The error was occuring on "Set pt=" line. Commented Sep 2, 2013 at 15:57
  • I suspect something happened to your source range, perhaps a column header (or an entire column?) was deleted by accident? Commented Sep 2, 2013 at 16:19

1 Answer 1

2

Try using a ListObject ("Table") instead of a Range for a datasource; these always are a valid data source for a pivot table - see this SO question - the OP there wasn't having the same problem as you did, but I believe the same solution applies.

The reason why tables are always valid sources is because they always have valid headers, and they always have at least 1 row of data.

Once you have turned your range into a table (and called it, say, "Table1"), you can use it like this:

Set cacheOfPt1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Table1")

With a cache that's using a valid data source, there shouldn't be a problem doing exactly what you're doing to Set pt:

Set pt = ActiveSheet.PivotTables.Add(cacheOfpt1, Range("a1"), "PivotTable1")

Also, with this setup you might want to reconsider the need to completely rebuild the pivot table from scratch every time; you can simply add/remove data to/from "Table1" and then refresh your pivot table - it will automatically be updated to reflect the contents of "Table1".

Sign up to request clarification or add additional context in comments.

7 Comments

Although an excellent suggestion, this isn't a specific answer to this specific question. It would make a good comment.
Thanks retailcoder! I'll check your suggestion. I've seen this problem faced by many. But the solution were not applicable to me.
Yeah, lots of pesky little things can go wrong when using a Range as a data source :) -- thanks for the tickmark!
Got my +1 too. Nice explanation of a better approach.
@B.ClayShannon you need to .Add to the sheet's .ListObjects collection. Specifically how that's done, certainly has a few Q&A's on this site. Or you can do it manually by selecting any cell in your table (range), and click "format as table" from the home ribbon tab.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.