【发布时间】:2014-12-16 03:40:39
【问题描述】:
下面的 VBA 代码应该建议用户输入报告的月份,然后相应地更新所有数据透视表。 我面临的问题是,如果用户输入的月份不是枢轴项目之一(即“mayo”i/o“may”),它将弄乱我的数据透视表。所以我想检查输入框中输入的字符串是否是正确的pivotItems之一。
我收到一条错误消息,指出“需要对象” 当我声明我的 bool 时,我没有发现有什么问题。
有什么想法吗?
Sub ChooseMonth55()
Dim MyMONTH As Variant
Dim MyRepMon As String
Dim Pi As Boolean
Set Pi = ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").PivotItems
Pi = False
InputBoxMyMONTH: MyMONTH = Application.InputBox("Enter the MONTH in a full text format")
MyRepMon = MyMONTH
Sheets("All results excl not tested").Select
If MyRepMon = Pi Then
MyRepMon = True
End If
If MyRepMon = True Then
ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").CurrentPage = MyRepMon
ActiveSheet.PivotTables("PivotTable5").PivotFields("Test Month").CurrentPage = MyRepMon
ActiveSheet.PivotTables("PivotTable6").PivotFields("Test Month").CurrentPage = MyRepMon
ActiveSheet.PivotTables("PivotTable7").PivotFields("Test Month").CurrentPage = MyRepMon
ActiveSheet.PivotTables("PivotTable13").PivotFields("Test Month").CurrentPage = MyRepMon
ActiveSheet.PivotTables("PivotTable8").PivotFields("Test Month").CurrentPage = MyRepMon
Else: MsgBox "There is a typo, please enter again the Month"
GoTo InputBoxMyMONTH
End If
End Sub
谢谢
【问题讨论】:
-
设置 Pi 然后让它像这样背靠背地设置为假有什么意义?
-
这可能是错误的抱歉,我在想我们必须声明布尔值并将其设置为 false 才能使代码正常工作。
-
什么样的值在这里结束?:'ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").PivotItems'??
-
我想将 Pi 声明为所有可能的枢轴项目,具体来说是一月、二月、五月……
-
我想确保输入框的结果是我pivot中的月份之一