PivotTable autofilter from defined name range

Spikenaylor

Board Regular
Joined
Apr 14, 2013
Messages
115
Office Version
  1. 365
Platform
  1. Windows
Hi

My code on worksheet activate is
Code:
Private Sub Worksheet_activate()    Dim pt As PivotTable
    Dim RolePick As Range
    Set RolePick = [Batch_ChartNumbers]
       For Each pt In Sheets("Debox Flock report").PivotTables
        Filter_PivotField pvtField:=pt.PivotFields("Batch Report"), varItemList:=Application.Transpose(RolePick)
    Next pt


End Sub

The defined name range "Batch_ChartNumbers" is populated by the end user and a report is generated, once the report is generated, the spreadsheet closes deleting the end users inputs, next time the spreadsheet is open the end users enters new data.

Works great until the defined name range doesn't contain anything and the report button is pressed or the end user clicks on the pivottable worksheet.

tried a few methods to fix, but can't get it to filter to blanks or nothing until the next time the defined name range is poulated, it keeps the previous user entered data in the pivot table "Batch Report" field.

Could anyone help please
Thanks very much
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Found different code to do the same job, got it to work with just one value in the range, but if the range is empty, the object does not exist and the code bombs out. I just need to work out, if the defined name range is actually empty (therefore does not exist), I need the pivot table to filter down to blanks or nothing. at the moment it retains previous data.
If the defined name range contains one or more values, then everything works correctly.

Code:
Private Sub Worksheet_activate()
 
    Dim pt As PivotTable, test As Boolean
   
    Set pt = ActiveSheet.PivotTables("PivotTable1")
       
    If [Batch_ChartNumbers].Cells.Count = 1 Then
        Filter_PivotField pvtField:=pt.PivotFields("Batch Report"), varItemList:=Array([Batch_ChartNumbers].Value)
    Else
        Filter_PivotField pvtField:=pt.PivotFields("Batch Report"), varItemList:=Application.Transpose([Batch_ChartNumbers])
    End If
 
 
End Sub

Code:
Function Filter_PivotField(pvtField As PivotField, varItemList As Variant)
    Dim strItem1 As String
    Dim i As Long
    On Error GoTo Errorhandler:
    Application.ScreenUpdating = False
 
    strItem1 = varItemList(LBound(varItemList))
        With pvtField
        .PivotItems(strItem1).Visible = True
        For i = 1 To .PivotItems.Count
            If .PivotItems(i) <> strItem1 And _
                  .PivotItems(i).Visible = True Then
                .PivotItems(i).Visible = False
            End If
        Next i
        For i = LBound(varItemList) + 1 To UBound(varItemList)
            .PivotItems(varItemList(i)).Visible = True
        Next i
    End With
    Exit Function
Errorhandler:
    MsgBox "Error while trying to process item: " & varItemList(i)
End Function

Error Message is

Run-Time Error '424':
Object Required
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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