Ben*_*Ben 5 excel vba excel-vba
我有两个源表,以及基于它们的几十个支点.
这两个表有一个共同的字段,具有一组可能的共同值.
我有两个切片器(每个源表一个).每个切片器控制许多相关的数据透视表.
我希望能够同步它们.
也就是说,如果用户在Slicer_1中选择值A,则Slicer_2会自动更新以同时选择值A.
所以到目前为止我所拥有的是非常基本的
ActiveWorkbook.SlicerCaches("Slicer_1").SlicerItems("A").Selected = ActiveWorkbook.SlicerCaches("Slicer_2").SlicerItems("A").Selected
ActiveWorkbook.SlicerCaches("Slicer_1").SlicerItems("B").Selected = ActiveWorkbook.SlicerCaches("Slicer_2").SlicerItems("B").Selected
ActiveWorkbook.SlicerCaches("Slicer_1").SlicerItems("C").Selected = ActiveWorkbook.SlicerCaches("Slicer_2").SlicerItems("C").Selected
现在,当slicer_1发生变化时,我将如何自动触发它?我已将宏指定给slicer_2,但在单击切片器框之前不会进行更新.
如何在执行所有更改之前延迟执行.此时它更新A字段(选择是/否)刷新我的表并移动到B等.
我希望它等待刷新,直到所有切片器字段都已更新
谢谢
同步切片器可以通过通用方式完成。
对于“通用”,我的意思是不应该依赖于(字面意思)切片器缓存名称,并且同步可以从任何切片器缓存开始。
实现这一切的方法是保存所有切片器缓存对象的状态。在数据透视表(位于一个或多个切片器高速缓存之下)发生更改后,可以将新状态与旧状态进行比较,并识别更新的高速缓存。从那里可以完成同步。
我的解决方案包含 4 个步骤:
1) create clsWrapperCache,Excel SlicerCache 对象的包装类
2) create clsWrapperCaches,clsWrapperCache 对象的集合类
3) create clsCacheManager,用于处理 SlicerCache 对象状态的管理器类
4) ThisWorkbook,设置对管理器的调用
1) clsWrapperCache,Excel SlicerCache 对象的包装类
' wrapper class around Excel SlicerCache object
Option Explicit
Public Object As SlicerCache
Public OldState As String
Public Function CurrentState() As String
' state is set by:
' a) name of first visible slicer item
' b) number of visible slicer items
Dim s As String
If Object.VisibleSlicerItems.Count > 0 Then
s = Object.VisibleSlicerItems.Item(1).Name
Else
s = ""
End If
s = s & vbCrLf ' separator that cannot be found in a SlicerItem name
s = s & CStr(Object.VisibleSlicerItems.Count)
CurrentState = s
End Function
Run Code Online (Sandbox Code Playgroud)
clsWrapperCache包含一个 Excel SlicerCache 对象。
更重要的是:它可以管理 SlicerCache 的状态。获取状态可以非常快地完成,即通过连接:
OldState最初在例程中设置(步骤 3) ,如果同步过程中涉及切片器高速Set_Caches缓存,则可以在例程中重置(步骤 3)。Synchronize_Caches
2)clsWrapperCaches,clsWrapperCache对象的集合类
' clsWrapperCaches, collection class of clsWrapperCache objects
Option Explicit
Private mcol As New Collection
Public Sub Add(oWC As clsWrapperCache)
mcol.Add oWC, oWC.Object.Name
End Sub
Public Property Get Item(vIndex As Variant) As clsWrapperCache
' vIndex may be of type integer or string
Set Item = mcol(vIndex)
End Property
Public Property Get Count() As Integer
Count = mcol.Count
End Property
Run Code Online (Sandbox Code Playgroud)
这是一个简单的集合类,仅保存clsWrapperCache对象。它将用于保存AllCaches集合中的对象。
3)clsCacheManager,处理SlicerCache对象状态的类
Option Explicit
Public AllCaches As New clsWrapperCaches
Public Sub Set_Caches()
Dim sc As SlicerCache
Dim oWC As clsWrapperCache
Dim i As Integer
If Me.AllCaches.Count <> ThisWorkbook.SlicerCaches.Count Then
' a) on Workbook_Open event
' b) maybe the user has added/deleted a Slice Cache shape by hand
Set AllCaches = New clsWrapperCaches
For Each sc In ThisWorkbook.SlicerCaches
'create a wrapper SlicerCache object
Set oWC = New clsWrapperCache
Set oWC.Object = sc
'save current state of SlicerCache into OldState
oWC.OldState = oWC.CurrentState
' add wrapper object to collection
AllCaches.Add oWC
Next
End If
End Sub
Sub Synchronize_Caches()
' copy current selections from slicer caches "FromCaches" into any other slicer cache with same SourceName
On Error GoTo ErrEx
Dim oWCfrom As clsWrapperCache
Dim oWCto As clsWrapperCache
Dim scFrom As SlicerCache
Dim scTo As SlicerCache
Dim si As SlicerItem
Dim i As Integer
Dim j As Integer
Application.EnableEvents = False ' prevent executing Workbook_SheetPivotTableUpdate event procedure
Application.ScreenUpdating = False
For i = 1 To Me.AllCaches.Count
Set oWCfrom = Me.AllCaches.Item(i)
If oWCfrom.CurrentState <> oWCfrom.OldState Then
Set scFrom = oWCfrom.Object
For j = 1 To Me.AllCaches.Count
Set oWCto = Me.AllCaches.Item(j)
Set scTo = oWCto.Object
' Debug.Print oWCto.Name
If scTo.Name <> scFrom.Name And scTo.SourceName = scFrom.SourceName Then
scTo.ClearAllFilters ' triggers a Workbook_SheetPivotTableUpdate event
On Error Resume Next
For Each si In scFrom.SlicerItems
scTo.SlicerItems(si.Name).Selected = si.Selected
Next
On Error GoTo 0
' update old state of wrapper object oWCto
oWCto.OldState = oWCto.CurrentState
End If
Next
' update old state of wrapper object oWCfrom
oWCfrom.OldState = oWCfrom.CurrentState
End If
Next
Ex:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
ErrEx:
MsgBox Err.Description
Resume Ex
End Sub
Run Code Online (Sandbox Code Playgroud)
Set_CachesclsCacheManager 类使用方法和管理缓存状态Synchronize_Caches。
Set_Caches:如果 ThisWorkbook 中的缓存数量与 AllCaches 的数量不同,则(重新)构建 AllCaches 集合。由此OldState保存每个切片器高速缓存。
Synchronize_Caches:这里遍历所有缓存。如果切片器缓存已更新 ( oWCfrom.CurrentState <> oWCfrom.OldState),则具有相同 SourceName(例如“年份”)的任何其他缓存也将更新。更新是通过将切片器项的所有选择从源缓存复制到目标缓存来实现的。OldState在同步过程结束时,所有涉及的缓存都会重置为当前状态。
4) ThisWorkbook,设置对缓存管理器的调用
Option Explicit
Private mCacheManager As New clsCacheManager
Private Sub Workbook_Open()
SetCacheManager
mCacheManager.Set_Caches
End Sub
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
SetCacheManager
mCacheManager.Set_Caches
mCacheManager.Synchronize_Caches
End Sub
Private Sub SetCacheManager()
If mCacheManager Is Nothing Then
Set mCacheManager = New clsCacheManager
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
SetCaches步骤 1 到步骤 3 的所有好处都可以在步骤 4 中获得:我们可以像或那样调用 CacheManagerSynchronize_Caches。这段代码很容易阅读。
该解决方案的优点:
clsCacheManager可以扩展类来处理切片器缓存之间的依赖关系。