Excel VBA数据透视表布尔值

时间:2014-12-15 17:39:41

标签: excel vba excel-vba

以下VBA代码应建议用户输入报告的月份,然后相应地更新所有数据透视表。 我面临的问题是,如果用户输入的月份不是其中一个枢轴项目(即" mayo" i / o"可能")它将弄乱我的数据透视表。所以我想检查输入框中输入的字符串是否是正确的pivotItem之一。

我有一条错误消息,说明"需要对象" 当我宣布我的布尔时,我没有发现什么问题。

有什么想法吗?

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

由于

2 个答案:

答案 0 :(得分:0)

尝试使用此功能。它确定数据透视表项是否位于数据透视表内的特定数据透视表字段中。

Public Sub Test_IsItemInPivotField()
  Debug.Print IsItemInPivotField("May", "Test Month", ActiveSheet.PivotTables("PivotTable1"))
  Debug.Print IsItemInPivotField("Mayy", "Test Month", ActiveSheet.PivotTables("PivotTable1"))   
End Sub

Public Function IsItemInPivotField(itemName As String, fieldName As String, pvt As PivotTable)
  Dim pvtItem As PivotItem

  For Each pvtItem In pvt.PivotFields(fieldName).PivotItems
      If pvtItem.Name = itemName Then
          IsItemInPivotField = True
          Exit Function
      End If
  Next

  IsItemInPivotField = False
End Function

答案 1 :(得分:0)

我建议您使用UserForm和下拉框,或者使用comboBox 将用户的输入限制为仅知道您工作的月份

  • 创建工作表,您可以隐藏它。插入您想要列出的月份。

  • 插入用户窗体

  • 将其命名为,将ComboBox插入其中,然后插入标签。

  • 将“属性”窗口中ComboBox的Rowsource设置为列出有效月份条目的范围。

SETUP 1

SETUP 2

现在右键单击VBA中资源管理器窗口中的用户表单。 插入此代码:

Private Sub ComboBox1_Change()
    Call ChooseMonth(ComboBox1.Value)
End Sub

您现在可以简化原始代码了,因为它会在没有错误的情况下拉出正确的月份,除非您在月份来源表上输入错误。

您的代码:

Sub ChooseMonth(myMonth As String)

    Sheets("All results excl not tested").Select

    ActiveSheet.PivotTables("PivotTable4").PivotFields("Test Month").CurrentPage = myMonth
    ActiveSheet.PivotTables("PivotTable5").PivotFields("Test Month").CurrentPage = myMonth
    ActiveSheet.PivotTables("PivotTable6").PivotFields("Test Month").CurrentPage = myMonth
    ActiveSheet.PivotTables("PivotTable7").PivotFields("Test Month").CurrentPage = myMonth
    ActiveSheet.PivotTables("PivotTable13").PivotFields("Test Month").CurrentPage = myMonth
    ActiveSheet.PivotTables("PivotTable8").PivotFields("Test Month").CurrentPage = myMonth

End Sub