Runtime error "1004" Unable to set the Position property of the PivotItem Class

kht0307

New Member
Joined
Jul 19, 2024
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi, I have the following code, that results in the above error when it hits the highlighted line:

For Each WS In ActiveWorkbook.Worksheets
WS.Select
If ActiveSheet.Name Like "Pivot*" And ActiveSheet.Name <> "Pivot Current month" Then
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Period")
.PivotItems("Flash").Position = .PivotItems.Count
End With
End If
Next WS


Can anyone please advise me as to what is going wrong here.
 
Do you know how to view the immediate window ? Ctrl+G if its not visible.
What do you get if you add the blue lines below:
(it will probably error out on the 2nd one. Check that the worksheet name and pivot sheet name makes sense)

Rich (BB code):
Sub test()

For Each ws In ActiveWorkbook.Worksheets
    ws.Select
    If ActiveSheet.Name Like "Pivot*" And ActiveSheet.Name <> "Pivot Current month" Then

        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Period")
                Debug.Print ws.Name, ActiveSheet.PivotTables(1).Name
                Debug.Print .PivotItems("Flash").Name
        .PivotItems("Flash").Position = .PivotItems.Count
        End With
    End If
Next ws
End Sub
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
This is what I get in the immediate window when the above debug code runs:

1721396712818.png
 

Attachments

  • 1721396630310.png
    1721396630310.png
    83.5 KB · Views: 3
Upvote 0
OK. You have a filter applied to Period.
It looks like " .PivotItems.Count" is counting ignoring the filter but then when it uses that position it decides its not valid since its greater than the last visible position.

To test it take the filter off your Period column and run the code.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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