如何使多个数据透视项在Excel VBA中可见
我在“主”表中有一个透视表。在如何使多个数据透视项在Excel VBA中可见,vba,excel,excel-2010,pivot-table,Vba,Excel,Excel 2010,Pivot Table,我在“主”表中有一个透视表。在数据透视字段的“报告过滤器”中,我有一个包含200个国家的“国家代码”。我想使用InputBox使该过滤器中的多个国家/地区可见 问题是我需要手动选择至少一个国家/地区或所有国家/地区,然后运行此程序。我无法通过这样做获得正确的数据。 我需要取消选择所有国家,然后我需要运行 我的代码 Sub Addcountries() Dim ws As Worksheet Dim str1 As Variant Dim Data As Variant Dim pf As Pi
数据透视字段
的“报告过滤器”中,我有一个包含200个国家的“国家代码”。我想使用InputBox使该过滤器中的多个国家/地区可见
问题是我需要手动选择至少一个国家/地区或所有国家/地区,然后运行此程序。我无法通过这样做获得正确的数据。
我需要取消选择所有国家,然后我需要运行
我的代码
Sub Addcountries()
Dim ws As Worksheet
Dim str1 As Variant
Dim Data As Variant
Dim pf As PivotField
Dim target As PivotTable
Set ws = Sheets("Main")
str1 = Application.InputBox("Enter the Country - comma separated")
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
If InStr(1, str1, ",") > 0 Then
Data = Split(str1, ",")
For i = LBound(Data) To UBound(Data)
ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(Data(i)).Visible = True
Next i
Else
ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(str1).Visible = True
End If
End If
End Sub
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
If InStr(1, str1, ",") > 0 Then ' more than 1 country >> create array
Data = Split(str1, ",")
Else ' single country
Data = Array(str1) '<-- create array with 1 element (for Match to work)
End If
' === You need a different loop, loop through all Pivot-Items, then look for a match with Data (array) ===
Dim pi As PivotItem
' clear previous Filter
ws.PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters
For Each pi In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems
' check if current Pivot-Item equals one of the elements of the array (use Match function)
If Not IsError(Application.Match(pi.Name, Data, 0)) Then ' Match successful
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End If
' rest of your code
您可以循环查看
数据透视项
集合,并检查每个数据透视项.Name
是否与数据
数组中的一个选定国家匹配-您可以使用匹配
函数来完成此操作
代码
Sub Addcountries()
Dim ws As Worksheet
Dim str1 As Variant
Dim Data As Variant
Dim pf As PivotField
Dim target As PivotTable
Set ws = Sheets("Main")
str1 = Application.InputBox("Enter the Country - comma separated")
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
If InStr(1, str1, ",") > 0 Then
Data = Split(str1, ",")
For i = LBound(Data) To UBound(Data)
ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(Data(i)).Visible = True
Next i
Else
ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems(str1).Visible = True
End If
End If
End Sub
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
If InStr(1, str1, ",") > 0 Then ' more than 1 country >> create array
Data = Split(str1, ",")
Else ' single country
Data = Array(str1) '<-- create array with 1 element (for Match to work)
End If
' === You need a different loop, loop through all Pivot-Items, then look for a match with Data (array) ===
Dim pi As PivotItem
' clear previous Filter
ws.PivotTables("MainTable").PivotFields("Country Code").ClearAllFilters
For Each pi In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems
' check if current Pivot-Item equals one of the elements of the array (use Match function)
If Not IsError(Application.Match(pi.Name, Data, 0)) Then ' Match successful
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End If
' rest of your code
如果str1=False,则
MsgBox“请输入一个国家”,“筛选国家”
出口接头
其他的
如果InStr(1,str1,“,”)>0,则“多个国家>>创建阵列
数据=拆分(str1,“,”)
别国
Data=Array(str1)“为了使过滤器正常工作,您需要为您不想看到的透视项提供假可见值,并为您所做的透视项提供真值,以便将所有透视项设置为真或假
这里有一些代码可以给你一个想法
Sub Addcountries()
Dim ws As Worksheet
Dim str1 As Variant
Dim Data() As Variant
Dim pf As PivotField
Dim target As PivotTable
Dim PivotItem As Object
Dim ShowMe As Boolean
Set ws = Sheets("Main")
str1 = Application.InputBox("Enter the Country - comma separated")
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
If InStr(1, str1, ",") > 0 Then
Data = Split(str1, ",")
Else
'Make Single Item Array
ReDim Data(1)
Data(0) = str1
End If
For Each PivotItem In ws.PivotTables("MainTable").PivotFields("Country Code").PivotItems
'Default Visibility Is False
ShowMe = False
For i = LBound(Data) To UBound(Data)
'Loop Through Each Item In Data To See If You Should ShowMe
ShowMe = (PivotItem.Name = Data(i))
If ShowMe Then
'Quit Early If You ShowMe
Exit For
End If
Next i
PivotItem.Visible = ShowMe
Next PivotItem
End If
End Sub
我不确定我是否理解,您是否收到此代码的错误?或者您缺少一个功能?我认为问题在于他在输入框中使用的数据。透视表使用excel中的实际参考值,而不是手动输入@Deepak:您是否尝试使用excel输入项目?@shai没有收到错误。我需要改进这个代码。我需要删除过滤器中的所有项目。@Deepak您需要保留一个,否则您将得到一个错误,您要保留哪一个?@shai是的,您是对的。但我不想先选择1。帮助我,你的代码几乎可以工作了。2个小问题。1) 当我选择一个国家时,它不起作用,仍然选择所有国家。第二次运行程序时,我需要清除过滤器。如何做that@Deepak我是根据你的帖子写的代码,我也会修改第二个选项。@现在当我只输入1个国家时,我会出错无法设置数据透视项类的visible属性pi.visible=False
@Deepak请再次检查,如果使用了ClearAllFilters
,并且在输入框中输入的值作为数据透视项之一存在,则不应出现此错误。试图隐藏最后可见的
透视项目时,将出现此错误