我在工作表“EX”中有一个数据透视表。在透视“报告过滤器”字段中,我有“国家代码”,其中包含200个国家/地区。我想使用InputBox从该过滤器中删除(隐藏)多于1个国家/地区。我使用下面的代码,它不起作用。它不是“If InStr”系列。帮帮我
Sub Removecountries()
Dim str1 As Variant
Dim Data As Variant
Dim ws As Worksheet
Set ws = Sheets("Ex")
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, ",")
With ws.PivotTables("RemoveTable").PivotFields("Country Code").PivotItems(Data(0)).Visible = False
End With
End If
End If
End Sub
答案 0 :(得分:1)
我刚刚测试了以下内容并且似乎有效,问题似乎是当您只在输入框中输入一个项目时:(在您尝试之前确保编辑" ws.PivotTable&# 34;我在测试时更改了名称)
Sub Removecountries()
Dim str1 As Variant
Dim Data As Variant
Dim ws As Worksheet
Set ws = Sheets("Ex")
str1 = Application.InputBox("Enter the Country - comma separated")
If str1 = False Then
MsgBox "Please Enter one Country", , "Filter Country"
Exit Sub
Else
ws.PivotTables("PivotTable1").PivotFields("CountryCode").ClearAllFilters
If InStr(1, str1, ",") > 0 Then
Data = Split(str1, ",")
For i = LBound(Data) To UBound(Data)
ws.PivotTables("PivotTable1").PivotFields("CountryCode").PivotItems(Data(i)).Visible = False
Next i
Else
ws.PivotTables("PivotTable1").PivotFields("CountryCode").PivotItems(str1).Visible = False
End If
End If
End Sub