Macro error when using Dim variable

blaksnm

Well-known Member
Joined
Dec 15, 2009
Messages
554
Office Version
  1. 365
Platform
  1. Windows
Hi guys
I try to update my pivot-tables (several) by:

Dim PRSK
PRSK = Range("ProsjektNrRegnskap").Value

PivotTables("PeTFakturert").PivotFields("HovedProsjektNr").ClearAllFilters
PivotTables("PeTFakturert").PivotFields("HovedProsjektNr").CurrentPage = PRSK ' error here
End If

But error occurs: "Run-time error 1004" Application-defined ot Object-defined error"
I used recording to programme and modyfied it afterwards.

Will someone guide me on this one?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Snoopy,
first of all, this is the page I generally get to when I'm stuck with VBA & Pivots: https://www.thespreadsheetguru.com/blog/2014/9/27/vba-guide-excel-pivot-tables
Next, the error occurs when you try to select an item in that's not in that field. So if the field has the items 1, 2 and 3 and you want to select 4 you'll get that error. There are two solutions:
Code:
'Option 1:
On Error Resume Next
PivotTables("PeTFakturert").PivotFields("HovedProsjektNr").CurrentPage = PRSK ' error here
On Error GoTo 0

'Option 2:
Set Sht = Worksheets("Sheet5")
PRSK = Sht.Range("K1").Value
Set piv = Sht.PivotTables("PivotTable1")

piv.PivotFields("a").ClearAllFilters
For Each pf In piv.PivotFields("a").PivotItems
    'This loops through all the values of field, compare here with PRSK
    Debug.Print pf.Value
Next pf
Hope that helps,
Koen
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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