Excel Combobox _Click在数据透视表刷新时激活

时间:2017-08-18 14:31:13

标签: excel vba excel-vba combobox excel-2013

我在同一张表上有一个comboBox和几个数据透视表。 comboBox根据所选内容更新数据透视表过滤器。

问题是,当数据透视表刷新(设置新过滤器)时,它将激活组合框_click功能,开始循环。

有解决方法吗?为什么数据透视表会刷新激活组合框?

这是_Click事件中的代码:

Private Sub cmb_SkuSelect_Click()
    Dim xlSheetSort As Worksheet
    Dim lastRow As Long
    Dim xlCell As Range
    Dim skuValue As Integer

    Set xlSheetSort = ActiveWorkbook.Worksheets("Sort")

    lastRow = xlSheetSort.Range("A1").End(xlDown).Row

    With xlSheetSort.Range("B1:B" & lastRow)
        Set xlCell = .Find(cmb_SkuSelect.Value, LookIn:=xlValues)
        If Not xlCell Is Nothing Then
            skuValue = xlSheetSort.Range("A" & xlCell.Row).Value
        End If
    End With

    updatePivot skuValue
End Sub

updatePivot:

Public Sub updatePivot(ByVal sku As Integer)
    Dim pt As PivotTable
    Dim pt2 As PivotTable
    Dim Field As PivotField
    Dim Field2 As PivotField
    Dim newSku As String

    Set pt = Worksheets("Sku Inventory").PivotTables("SkuInfo")
    Set Field = pt.PivotFields("Sku Number")

    Set pt2 = Worksheets("Sku Inventory").PivotTables("InventoryInfo")
    Set Field2 = pt2.PivotFields("Sku Number")

    newSku = sku

    With pt
        Field.CurrentPage = newSku
        pt.RefreshTable
    End With

    With pt2
        Field2.CurrentPage = newSku
        pt2.RefreshTable
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

像这样发誓

Public Sub updatePivot(ByVal sku As Integer)
    Dim pt As PivotTable
    Dim pt2 As PivotTable
    Dim Field As PivotField
    Dim Field2 As PivotField
    Dim newSku As String

    Set pt = Worksheets("Sku Inventory").PivotTables("SkuInfo")
    Set Field = pt.PivotFields("Sku Number")

    Set pt2 = Worksheets("Sku Inventory").PivotTables("InventoryInfo")
    Set Field2 = pt2.PivotFields("Sku Number")

    newSku = sku

    With pt
        With Field
            .Orientation = xlHidden
            pt.RefreshTable
            .Orientation = xlPageField
            .CurrentPage = newSku
        End With
    End With

    With pt2
        With Field2
            .Orientation = xlHidden
            pt2.RefreshTable
            .Orientation = xlPageField
            .CurrentPage = newSku
        End With

    End With
End Sub