I am trying to make a macro that will automaticly adjust the filters for several pivot tables based on user input, however if the user puts in something that isn't avaialble the code produces an error when trying to apply the filter.
Is there anyway to check which filters are available to select?
Example: One pivot table has three filters available (Year, Month, Type [Complaint, Praise, Both]) but if during a month there weren't any complaints then there is an error.
Code:
With PTable
.PivotFields("Year").CurrentPage = Y
.PivotFields("Month").CurrentPage = M
.PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
End With
Further to my comments, try this
With PTable
.PivotFields("Year").CurrentPage = Y
.PivotFields("Month").CurrentPage = M
On Error Resume Next
.PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
If Err.Number <> 0 Then
Msgbox "Filter Didn't get applied"
End If
On Error GoTo 0
End With
Related
I have this code to pick a date and change the filter of a Pivot Table to reflect the related info. But some times this works and some times it gives me the
Error 1004 Application-defined or Object-defined error
This is driving me crazy, and I donĀ“t know what's happening, especially because this code works and then it doesn't, without any change.
Dim DataVenda As Date
DataVenda = InputBox("Data de Vendas (dd/mm):")
ActiveSheet.Range("B1").Select
With Selection
ActiveSheet.PivotTables("DinTblResumoDiario").PivotFields("Data:").ClearAllFilters
ActiveSheet.PivotTables("DinTblResumoDiario").PivotFields("Data:").CurrentPage = DataVenda
End With
The error is in the last command: ActiveSheet.PivotTables("DinTblResumoDiario").PivotFields("Data:").CurrentPage = DataVenda
Like written above, it's better to avoid using ActiveSheet, Select and Selection, and use fully qualified objects instead.
The code below I use an object of type PivotTable to define and set the Pivot Table, and also for the PivotField.
I also added another type of InputBox that forces the user to enter a value in a Date format.
Note: there is a chance that the value selected in the InputBox isn't found anywhere inside the Pivot Table, so I've added a way to check it in the code below.
Code
Option Explicit
Sub OccupancyPivot()
Dim wsSheet As Worksheet
Dim PT As PivotTable
Dim PTFld As PivotField
Dim DataVenda As Date
' use this type of Input Box to force the user to enter a date format
DataVenda = Application.InputBox("Data de Vendas (dd/mm):", "Select date", FormatDateTime(Date, vbShortDate), Type:=1)
Set wsSheet = ThisWorkbook.Worksheets("Sheet1") ' <-- modify "Sheet1" to your Pivot's sheet name
On Error Resume Next
Set PT = wsSheet.PivotTables("DinTblResumoDiario") ' set the PivotTable
On Error GoTo 0
If PT Is Nothing Then ' "DinTblResumoDiario" Pivot Table doesn't exist
MsgBox "Pivot Table 'DinTblResumoDiario' doesn't exist!"
Else ' "DinTblResumoDiario" Pivot Table exists
Set PTFld = PT.PivotFields("Data:") ' <-- set the pivot field
PTFld.ClearAllFilters
On Error Resume Next
PTFld.CurrentPage = DataVenda ' Error line if DataVenda isn't valid (no value inside Pivot Table matches it)
If Err.Number <> 0 Then
MsgBox "Filter Didn't get applied, no value inside Pivot Table matches your selection"
End If
On Error GoTo 0
End If
End Sub
The data source will change often. After I refresh the pivot table, I have the following code to update the grade pivot item field. Sometimes grade 4 is available and sometimes it is not. Essentially, If grade 4 is available I want it to be selected and if it is not available then all fields can be select. For some reason when I run it, it stops on the else line. Any suggestions?
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").ClearAllFilters
If IsError(ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4") Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = _
"(All)"
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4"
End If
You can't trap the PivotField("Grade").CurrentPage error with IsError.
Try the code below:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").ClearAllFilters
On Error Resume Next
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4"
If Err.Number <> 0 Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = _
"(All)"
On Error GoTo 0
End If
Note: you could clean up your code if you use With statements. In your code, you could use With ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade")
I have a pivot table in which im trying to apply a filter with the following code:
Dim Brand_CodePF As pivotField
Set Brand_CodePF = BrandSellingSTItem_SalesWS.PivotTables("Brand SellingState Item Pivot sales").PivotFields("BrandCode")
With Brand_CodePF
.ClearAllFilters
.CurrentPage = dataBrand
End With
Dim Selling_StatePF As pivotField
Set Selling_StatePF = BrandSellingSTItem_SalesWS.PivotTables("Brand SellingState Item Pivot sales").PivotFields("SellingSTCode")
With Selling_StatePF
.ClearAllFilters
**.CurrentPage = GLSellingState_**
On Error Resume Next
End With
The line i have highligted with with * * is giving me an error. I have checked my data , it seems the value upon which i want to filter doesn't exist in that pivot that is, GLSellingState is not in that pivot, however i want to handle such a case , i want my application to simply move on to the next iteration of my code ( I have all this inside my loop) how do i achieve this?
Dear Stackoverflow users,
I am having difficulty with something that seems simple enough to not cause this much trouble. I am trying to filter a Pivot Table based on the greater than equal to criteria, mentioned in the code below. Here is the code:
Sub KEEP_ON_FILE_DT_CLICK()
Dim caches As Excel.SlicerCaches
Set cache = ActiveWorkbook.SlicerCaches("Slicer_KEEP_ON_FILE_DT")
With cache
For i = 1 To .SlicerItems.Count
If .SlicerItems(i).Selected Then
SelectedItem = .SlicerItems(i).Value
Exit For
End If
Next i
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT"). _
PivotFilters.Add Type:=xlValueIsGreaterThanOrEqualTo, Value1:=SelectedItem
End Sub
For some reason, the last line where I actually apply the filter keeps giving me the following error message:
Run time error '5':
Invalid Procedure Call or argument
Kindly assist.
Thanks
I assume that your pivot table does not allow for any such filters to be applied. There are many different ways / types of pivot tables and not all of them allow for such filters as xlValueIsGreaterThanOrEqualTo.
Yet, the following code should work under all circumstances:
Dim itmList As PivotItem
Dim dblDesiredValue as Long
dblDesiredValue = 32
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT").EnableMultiplePageItems = True
For Each itmList In ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT").PivotItems
Select Case CDbl(itmList.Name)
Case Is >= dblDesiredValue
itmList.Visible = True
Case Else
itmList.Visible = False
End Select
Next itmList
Hello am trying to do a code like below
Do while (the selected cell is in the column b)
Code...
End Loop
Message error
When I click the button when the selected cell is not in the column b want to appear error msg
Provided that a single cell is selected, then you can achieve the desired result by using the following statement:
' get the column number
col = Selection.Column()
Column "B" corresponds to Column number 2. If selection is not in a column 2, then pop-up the Error Message (as per your requirement).
Hope this will help.
Based on your comment, you can try something like this:
Dim r As Range
'assign the selection to a variable
'but make sure it is a range
If TypeName(Selection) = "Range" Then Set r = Selection
'check if something is assigned to r; meaning a range is selected
If Not r Is Nothing Then
'check if it is somewhere in Column B
If Not Intersect(r, Sheets("Sheet1").Range("B:B")) Is Nothing Then
'do your stuff here
Else
'your error message here
MsgBox "Invalid Selection", vbCritical
End If
End If
Above code uses MsgBox to act as your error message.
Is this what you're trying? HTH.