更改切片器项目时,宏会在50%的时间内触发

时间:2018-08-20 14:46:51

标签: excel vba excel-vba slicers

我有一个特殊的问题,在互联网上的任何地方都找不到任何解决方案。

所以我有一个连接到6个切片器的数据透视表,还有一个数据范围取决于数据透视表值的图表。

我创建了一个宏,该宏在每次工作表单元格中的任何值更改时都会更新图表比例。这是宏:

Public Sub worksheet_Change(ByVal Target2 As Range)
If ActiveSheet.Name = "Dashboard" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DataEntryMode = xlOff
 'Chart_axis Macro

Sheets("Dashboard").ChartObjects("Chart 9").Activate
If ActiveSheet.Range("B19") = "excluding CE" Then
    ActiveChart.Axes(xlValue).MinimumScale = Range("E3").Value
    ActiveChart.Axes(xlValue).MaximumScale = Range("E4").Value
Else
    ActiveChart.Axes(xlValue).MinimumScale = Range("A3").Value
    ActiveChart.Axes(xlValue).MaximumScale = Range("A4").Value
End If

ActiveChart.Refresh
ActiveSheet.Range("B18").Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End If

End Sub

为了按预期工作,我还必须创建一个函数来读取切片器的活动元素:

Public Function GetSelectedSlicerItems(SlicerName As String) As String
Application.Volatile
Set coll = New Collection
Dim cache As Excel.SlicerCache
Dim i As Integer
Set cache = ActiveWorkbook.SlicerCaches(SlicerName)
Dim sItem As Excel.SlicerItem
Dim result As String
For Each sItem In cache.SlicerItems
If sItem.Selected And sItem.HasData Then
    'Debug.Print sItem.Name
    'Debug.Print sItem.HasData
    'GetSelectedSlicerItems = (sItem.Name)
    coll.Add sItem.Name
    End If
Next sItem
For i = 1 To coll.Count
'Debug.Print coll(i)
 result = result & coll(i) & ", "
Next i
result = Left(result, Len(result) - 2)
GetSelectedSlicerItems = result
End Function

我的问题是,当切片器项目更改时,函数的值始终会更新,而宏仅在50%的时间中随机执行它。

我的报告的屏幕截图:

img1

包含所选切片器项目功能的公式位于右上角。

那么您是否知道如何使其100%地工作?

预先感谢

艾伦

编辑:我忘了补充一点,只有高亮一个切片器才是问题。当我选择多个切片器(使用ctrl + click)时,它始终有效。

0 个答案:

没有答案