我正在尝试使组合框的值成为数据透视表过滤的依赖项。如果组合框的值与数据透视表的过滤器部分匹配,则下面的代码可以正常工作,但是情况并非如此,因此会导致错误。
Private Sub UserForm_Initialize()
ComboBox1.AddItem "3(Facility Approved),4(Bid Appn Approved),12(Cancelled With Outs)"
ComboBox1.AddItem "3"
End Sub
Private Sub CommandButton1_Click()
Call Macro5
Dim sht As Worksheet, pflds As PivotFields, showItems As Boolean
Dim arr
With Worksheets("CT")
.Range("G1").Value = TextBox2.Value
.Range("C1").Value = TextBox1.Value
End With
arr = Split(ComboBox1, ",") 'make an array from the combobox value
'show only the values in arr for specific pivot fields
ShowOnlyThese Sheets("BP").PivotTables("PivotTable1").PivotFields("Facility_Status_Id"), arr
ShowOnlyThese Sheets("BC").PivotTables("PivotTable1").PivotFields("Facility_Status_Id"), arr
ActiveWorkbook.RefreshAll
Unload Me
ActiveWorkbook.RefreshAll
End Sub
这是我尝试修正此问题的代码,但出现错误。任何帮助将不胜感激。
Private Sub UserForm_Initialize()
'list to be chosen in the combobox
ComboBox1.AddItem "3(Facility Approved),4(Bid Appn Approved),12(Cancelled With Outs)"
ComboBox1.AddItem "3"
End Sub
Private Sub CommandButton1_Click()
Call Macro5
Dim sht As Worksheet, pflds As PivotFields, showItems As Boolean
Dim arr
With Worksheets("Connection Totals")
.Range("G1").Value = TextBox2.Value
.Range("C1").Value = TextBox1.Value
End With
If ComboBox1.Value = "3(Facility Approved),4(Bid Appn Approved),12(Cancelled With Outs)" Then
arr = Array("3", "4", "12") 'make an array from the combobox value
Else
arr = Array("3")
'show only the values in arr for specific pivot fields
ShowOnlyThese Sheets("By Participants").PivotTables("PivotTable1").PivotFields("Facility_Status_Id"), arr
ShowOnlyThese Sheets("By Corporates").PivotTables("PivotTable1").PivotFields("Facility_Status_Id"), arr
End If
ActiveWorkbook.RefreshAll
Unload Me
ActiveWorkbook.RefreshAll
End Sub
'loop over all items in a pivotfield shows only those matching a value
Sub ShowOnlyThese(pf As PivotField, arrItems)
Dim pi As PivotItem, haveOne As Boolean
For Each pi In pf.PivotItems
On Error Resume Next
pi.Visible = Not IsError(Application.Match(pi.Value, arrItems, 0))
On Error GoTo 0
Next pi
End Sub
答案 0 :(得分:1)
在立即窗格中:
? application.match("1", array(1,2,3), 0) '>> error 2042
? application.match("1",array("1","2","3"),0) '>> 1
枢轴字段值是字符串,但是要匹配的数组是数字。 如果您传递字符串数组,则应该可以。
您的ShowOnlyThese
有一个错误,当应该隐藏该字段值时,该字段值可能会保持可见,如果隐藏该值将意味着未显示任何值(必须至少显示一个)
If ComboBox.Value = "3(Facility Approved),4(Bid Appn Approved),12(Cancelled With Outs)" Then
arr = Array("3", "4", "12")
Else
arr = Array("3")
End If
ShowOnlyThese Sheets("By Participants").PivotTables("PivotTable1").PivotFields( _
"Facility_Status_Id"), arr
ShowOnlyThese Sheets("By Corporates").PivotTables("PivotTable1").PivotFields( _
"Facility_Status_Id"), arr