VBA to remove CalulatedField from Pivot Table - vba

I'm trying to write some code to add and then remove a calculated field to and from a Pivot Table. Below are the two pieces of code:
Sub AddPivotField()
With Worksheets(1).PivotTables("PivotTable1")
.AddDataField Worksheets(1).PivotTables( _
"PivotTable1").PivotFields("hProdUtil"), "Sum of hProdUtil"
.DataBodyRange.NumberFormat = "#0.0%"
End With
End Sub
And:
Sub RemovePivotField()
With Worksheets(1).PivotTables(1).DataFields("Sum of hProdUtil")
.Parent.PivotItems(.Name).Visible = False
End With
End Sub
The AddPivotField works fine but when I run the RemovePivotField I get the "Object doesn't support this property or method" error. Any ideas?
Thanks

Try the following code:
Sub RemovePivotField()
With Worksheets(1).PivotTables(1).DataFields("Sum of hProdUtil")
.Orientation = xlHidden
End With
End Sub

Was able to figure it out by modifying the code on https://www.thespreadsheetguru.com/blog/2014/9/27/vba-guide-excel-pivot-tables

Related

Merge cells of tables in the header in Word VBA

I would like to merge two cells of the second table in the header of my Word document. I created the script below but it has a run-time error: '5491'.The requested member of the collection does not exist.
The error occured on this line" With xTable(2)"
Sub mergercells()
Set xTable = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables
With xTable(2)
.Cell(Row:=3, Column:=2).Merge _
MergeTo:=.Cell(Row:=3, Column:=1)
.Borders.Enable = False
End With
End Sub
Thanks,
All you need is:
Sub MergeCells()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(2)
.Cell(Row:=3, Column:=2).Merge MergeTo:=.Cell(Row:=3, Column:=1)
.Borders.Enable = False
End With
End Sub

VBA Access Pivot Chart set the y-axis min/max scale

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

Having Run-time-error 1004 Application defined or object defined error

I have a simple formula in vba, like this :
Sub button_fu()
Windows("H1.xlsm").Activate
Sheets("jan").Activate
Range("J3").Select
Range("J3").Formula = "=IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,8,False))=1,""terhubung"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,9,False))=1,""unreach"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,10,False))=1,""reject"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,11,False))=1,""workload"","""")"
End Sub
anyone hepl me, how the problem solved it?
You are missing some closing brackets in your formula:
Sub button_fu()
Windows("H1.xlsm").Activate
Sheets("jan").Activate
Range("J3").Select
Range("J3").Formula = "=IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,8,False))=1,""terhubung"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,9,False))=1,""unreach"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,10,False))=1,""reject"",IF((VLOOKUP(C3,'[FOLLOW UP H1.xlsx]jan'!$C$8:$M$100,11,False))=1,""workload"",""""))))"
End Sub
P.S. Should "FOLLOW UP H1.xlsx" be "H1.xlsm", or do you have another workbook?

Unable to get PivotFields property of the PivotTable class

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

Excel VBA: clear items in pivot table

I am new to VBA...
I am trying to write a macro that will clear all the selections within a pivot table filter named "Product Family" and select only the item whose name is contained in cell "B33". I am referencing the pivot table in one sheet "sheet8" and trying to change a graph on "Dashboard".
Here is the code...
Sub thisisalsotemp()
'
' thisisalsotemp Macro
'
'
Sheets("Dashboard").Select
ActiveSheet.ChartObjects("Chart 1").Activate
Sheet8.PivotTables("capbylp").PivotFields("Product Family").PivotFields.ClearAllFilters
With Sheet8.PivotTables("capbylp").PivotFields("Product Family")
.PivotItems(Range("B33")).Visible = True
End With
End Sub
The error is in the following line:
Sheet8.PivotTables("capbylp").PivotFields("Product Family").PivotFields.ClearAllFilters
The error message is:
Object doesn't support this property or method
#SeanCheshire: Thanks for the help. I feel this is much closer to what I want. However, I couldnt get it to work. I played around with it a little bit and am closer. here is what i have...
Sub thisisalsotemp2()
Sheets("Dashboard").Select
Sheet8.PivotTables("capbylp").PivotFields("Product Family") = Range("B33")
End Sub
Error 1004 reads: unable to set the pivotfields property of the pivottable class
in the line: Sheet8.PivotTables("capbylp").PivotFields("Product Family") = Range("B33")
you need to set CurrentPage (and you shouldn't need to clear it first).
Using what is shown in your code, I would have something like:
Sheet8.PivotTables("capbylp").PivotFields("Product Family"). _
PivotFields("MyPivotField").CurrentPage = Range("B33").Value
(broken into 2 lines for readability)
This is slightly related; I wanted to clear multiple-selections whenever the user makes them. Apparently, setting the VisibleItemsList can do that.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xPF As PivotField
Dim nms As Variant
nms = Array("Calculation", _
"Rate Type", _
"xx Hierarchy")
Set xPT = Application.ActiveSheet.PivotTables(1)
For Each xPF In xPT.PageFields
For Each nm In nms
If xPF.Name Like "*" & nm & "*" Then
If UBound(xPF.VisibleItemsList) > 1 Then
xPF.VisibleItemsList = Array("")
End If
End If
Next nm
Next xPF
End Sub