Error when adding pivot table: "PivotTable field name is not valid. ..."

Anniemack2

New Member
Joined
Mar 7, 2019
Messages
3
Sheesh, I've liberally borrowed code from this website already, but am still getting an error when I go to insert pivot table. What am I missing?? Would appreciate any help!

Code:
    ws_NewBP.Activate    ' Activate worksheet

    Range("A2").Select
    lastrow = ws_NewBP.Cells.Find("", , xlFormulas, xlPart, xlByRows, xlPrevious).Row - 1   ' find last row of data
    ptrange = ws_NewBP.Name & "!R1C1:R" & lastrow & "C6"  ' format = "Sheetname!R1C1:R##C6"
    
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ptrange, Version:=6).CreatePivotTable _
    TableDestination:="Sheet3!R3C1", TableName:="PivotTable3", DefaultVersion:=6

Ann
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I wanted to add that when I do "Insert pivot table" from excel (not in the macro) with the same data in the very same file, it works beautifully. But the VBA code doesn't work. I also tried recording the steps, the only difference in the recorded code is the range specification. But the range that gets computed looks fine. :confused:

Please help!
 
Upvote 0
I found my issue - in typing my last response it became more clear that it HAD to be a problem with the Range. I was including a header row that I shouldn't have been. Problem resolved. Hurray!

I'd delete this post but I don't know how to.
 
Upvote 0

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top