VBA not finding PivotField

LeroyWork

New Member
Joined
Dec 12, 2019
Messages
4
Office Version
  1. 2016
Hi,
I've created a pivot table and can use some code to select the table range, row range and databodyrange. This works. However when I try to narrow the selection down to a specific Customer Number the error "Run-time error '1004': Unable to get the PivotFields property of the PivotTable class" occurs. I understand this is effectively saying there is no pivot field called "Customer Number" however this is how the pivot field source data is named and is the name showing in the pivot table. I've included a screen shot & code used below.
MrExcel.PNG

The code I'm using is:
Sub Lease()
Dim pt As PivotTable
Set pt = Sheets("pivots").PivotTables("Lease")
pt.TableRange1.Select ' this works
pt.DataBodyRange.Select ' this works
pt.RowRange.Select 'this works

End Sub

Sub test()
Dim pt As PivotTable
Set pt = Sheets("pivots").PivotTables("Lease")
'
pt.DataLabelRange.Select ' this errors
pt.PivotFields("Customer Number").PivotItems("11839.01").DataRange.Select ' this errors

End Sub

Any ideas as to why this won't work?

thanks in advance for any answers
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Board!

You must have a field in the Column area and also a field in the values area.

1576187518405.png
 
Upvote 0
Hi DanteAmor,

Thanks for the super quick reply. I rearranged the pivot table to put the Growing Area in the columns. This resulted in "pt.DataLabelRange.Select" working :) but is still erroring on the pt.PivotFields("Customer Number").PivotItems("11839.01").DataRange.Select. :(
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,390
Members
448,957
Latest member
Hat4Life

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