更改pivottable的pivotfiels的以下VBA代码正在运行,但我想添加一个步骤来检查输入框中输入的字符串是否是正确的pivotItem之一。
工作代码:
Sub ChooseMonth()
Dim MyMONTH As Variant
Dim MySTRING As String
MyMONTH = Application.InputBox("Enter the MONTH in a full text format")
MySTRING = MyMONTH
Sheets("All results excl not tested").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable5").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable6").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable7").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable13").PivotFields("Test Month").CurrentPage = MySTRING
End Sub
所以我尝试了不同的选项来检查输入的字符串是否是其中一个数据透视表,但还没有人正在使用。
任何想法。
Dim MyMONTH As Variant
Dim MySTRING As String
Set pi = ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").PivotItems
MyMONTH = Application.InputBox("Enter the MONTH in a full text format")
MySTRING = MyMONTH
Sheets("All results excl not tested").Select
If MySTRING = pi Then
ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable5").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable6").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable7").PivotFields("Test Month").CurrentPage = MySTRING
ActiveSheet.PivotTables("PivotTable13").PivotFields("Test Month").CurrentPage = MySTRING
Else
MsgBox "There is a typo, please enter again the Month"
MyMONTH = Application.InputBox("Enter the MONTH in a full text format")
End If
End Sub
非常感谢
答案 0 :(得分:0)
对于每个数据透视表,您需要遍历值,类似这样的
dim APivotItem as PivotItem
dim pt as pivottable
for each pt in ActiveSheet.PivotTables
for each APivotItem in pt.PageFields("Test Month").PivotItems
if APivotItem.name = MyMONTH then
' found in this pivot table
end if
next
next