VBA - test if a value is a valid selection for a PivotField

后端 未结 2 827
自闭症患者
自闭症患者 2021-01-22 00:24

For a pivot table (pt1) on Sheet1, I use VBA to change the value of a filter field (filterfield) using the code below. Let\'s say values for field can be A, B or C



        
2条回答
  •  孤街浪徒
    2021-01-22 00:49

    If you want to have a helper function and do this without looping values you may use visual basic OnErrorResumeNext trick.

    Private Function hasPivotItem(pField As PivotField, value As String) As Boolean
        On Error Resume Next
        hasPivotItem = Not IsNull(pField.PivotItems(value))
        On Error GoTo 0
    End Function
    
    ' somewhere in your vba program
    Debug.Print hasPivotItem(ptTable.PivotFields("Level"), "1")
    Debug.Print hasPivotItem(ptTable.PivotFields("Level"), "-123")
    

提交回复
热议问题