So I'm having some issues with my VBA and toggling a PivotTable filter. This is my code:
Sub Macro2()
Sheets("Report").Visible = True
Sheets("Report").PivotTables("PivotTable1").PivotCache.Refresh
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").ClearAllFilters
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").CurrentPage = "1"
Sheets("Report").Activate
End Sub
I've tried using "1", 1, 1.0 and haven't had any luck. The error I get is:
"Application-defined or object-defined error"
Any help is appreciated.
For some reason Excel 2013 might have been the issue here. I managed to find a workaround should anyone else run into this issue. I used a loop which would let me edit visibility of the fields. Here was the solution:
Sub CreateReport()
Sheets("Report").Visible = True
Sheets("Report").PivotTables("PivotTable1").PivotCache.Refresh
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").ClearAllFilters
Dim pi As PivotItem
For Each pi In Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").PivotItems
If pi.Value = 0 Then
pi.Visible = False
End If
Next pi
Sheets("Report").Activate
End Sub
Related
Came into another snag formating my Pivot Chart via VBA in Access 2010. I get the "Run-time error '438': Object doesn't support this property or method" error.
Again, I have tried many different iterations of the code, but can't get the right one. As always, thank you in advance for your assistance.
Private Sub Form_Load()
'Call LineMarkerFormat
RONMax = DMax("[Total A/C]", "tblRONChartComparison")
RONMin = DMin("[Total A/C]", "tblRONChartComparison")
With Me.ChartSpace.Charts(0).Axes(1).Scaling
.MinimumScale = RONMin
.MaximumScale = RONMax
End With
End Sub
and the very next site I check had the very simple solution. you no longer need the scaling on the end of the .minimum/.maximum. Here is my working code.
Private Sub Form_Load()
'Call LineMarkerFormat
RONMax = DMax("[Total A/C]", "tblRONChartComparison")
RONMin = DMin("[Total A/C]", "tblRONChartComparison")
With Me.ChartSpace.Charts(0).Axes(1).Scaling
.Minimum = RONMin
.Maximum = RONMax
End With
End Sub
I have a very simple VBA code where I want to first clear all filters in the slicer then select RB.
Below is the code I am using without success:
ActiveWorkbook.SlicerCaches("Slicer_Manufacturer1").ClearManualFilter
With ActiveWorkbook.SlicerCaches("Slicer_Manufacturer1")
.SlicerItems("RB").Selected = True
End With
End Sub
Can anyone help?
Try this and let me know what happens:
Dim cache As SlicerCache
For Each cache In ActiveWorkbook.SlicerCaches
cache.ClearManualFilter
Next cache
With ActiveWorkbook.SlicerCaches("Slicer_Manufacturer1")
.SlicerItems("RB").Selected = True
End With
End Sub
I have a weird error with excel vba. I am trying to set the visibility to false inside a pivot table for date values. My code works fine on a dummy table, but it return an error (1004) on my real table.
Sub MultiItemPivotFilter2()
Dim PI As PivotItem
Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1").RefreshTable
For Each PI In Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1").PivotFields("date2").PivotItems
If DateValue(PI.Name) < DateValue(Sheets("Pivot_stocks_1").Range("J13").Value) Then
PI.Visible = False
Else
PI.Visible = True
End If
Next PI
End Sub
The error occurs in line PI.Visible = False
(Unable to set Visible property of the PivotItem class)
Maybe this would help you:
unable to set the visible property of the pivotitem class
Excel sometimes does not remove old entries from pivotcache on which pivot table is based.In that some item may be there which is not in source data but excel keeps it in cache for optimizing.Refreshing cache does not solve issue.Only way is remove the cache and recreate it.
You should always do your select true loop and then do a select false loop. The reason being that Excel does them one at a time and will not allow you to have everything hidden in a pivot table, it will error out (or what have you) so:
Sub MultiItemPivotFilter2()
Dim PI As PivotItem
With Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1")
.RefreshTable
'Loop first setting all to "True"
For Each PI In .PivotFields("date2").PivotItems
PI.Visible = True
Next PI
'Loop then setting any "False"
For Each PI In .PivotFields("date2").PivotItems
If DateValue(PI.Name) < DateValue(Sheets("Pivot_stocks_1").Range("J13").Value) Then
PI.Visible = False
End If
Next PI
End With
End Sub
Hope that makes sense!
Edit
Even if it doesn't fix your problem you should always implement this logic when hiding pivotItems
I am trying to refer to the value of an ActiveX checkbox control in another worksheet. My goal is to make the value of the check box in my current workbook the same as the one in another open workbook.
I can do this between two checkboxes in the same workbook on different sheets:
Private Sub CommandButton1_Click()
If Sheets("Sheet2").Box2.Value = True Then
Box1.Value = True
Else: Box1.Value = False
End If
End Sub
But I'm receiving a Run-time error '9' "Subscript out of range" error when I run the following code:
Private Sub CommandButton2_Click()
If Worksheets("Book2").OLEObjects("Box3").Value = True Then
Box1.Value = True
Else: Box1.Value = False
End If
End Sub
The "If Worksheets" line is highlighted when I try to debug the code. I'm sure I'm referring to the other checkbox incorrectly, but I've searched high and low for the proper way to refer to this without any luck. Thank you in advance for your help!
If Worksheets("Book2").OLEObjects("Box3").Value = True Then
"Book2" is not the name of a Worksheet, presumably it is the name of the other book. In which case:
If WorkBooks("Book2").Worksheets(1).OLEObjects("Box3").Value = True Then
..or use whatever the name of the worksheet in the other book is called.
I'm trying to make a macro that changes the filters of several PivotTables (not all), but i'm getting an error on the PivotFields, here's a sample of my code:
Sheets("Sheet1").PivotTables("PivotTable" & pivot_counter).PivotFields( _
"[year].[year].[year]").VisibleItemList = Array("")
My questions are:
1- Why do we use PivotFields("[year].[year].[year]") when using VisibleItemList? Why do we have to repeat it and what's the meaning of it, couldn't anything about it anywhere.
2- What is supposedly wrong with this code? My pivot table has a field called "year" and the filter is set for a specific year (let's say 2013) and i wanted it change for all possible years.
Sub Tester()
ShowAll ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
End Sub
Sub ShowAll(pf As PivotField)
Dim pi As PivotItem
Application.ScreenUpdating = False
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
Application.ScreenUpdating = True
End Sub