保存并加载 Excel 切片器缓存
Save and Load Excel Slicer Cache
我遇到了一个简单的 VBA 片段,用于检查 Excel 工作簿中所有活动切片器的切片器缓存。
Sub RetrieveSlicers()
Dim caches As Excel.SlicerCaches
Set caches = ActiveWorkbook.SlicerCaches
End Sub
通过在 End Sub
旁边放置一个断点,右键单击 cache
并选择 'Add Watch...'
(见下文)
您可以通过 'Watches' window 查看每个活动切片器中的所有项目。
我的问题是,我能否保存切片器缓存信息(特别是 SlicerItems)供以后使用(可能作为文本数组?),然后将保存的切片器缓存加载回切片器(使用保存的 SlicerItems 重新填充切片器) ?
示例如下:
我确信获取切片器缓存数据一样容易,设置切片器也同样容易缓存数据。
非常感谢您一如既往的帮助。
先生J
像这样的东西应该可以工作(我现在没有任何东西要测试):
Sub Save_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim SliCName As String
Dim sliIt As Excel.SlicerItem
Dim A()
ReDim A(1 To 3, 1 To 1)
A(1, 1) = "Slicer Cache Name"
A(2, 1) = "Slicer Item Name"
A(3, 1) = "Selected"
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Set SliCaches = ActiveWorkbook.SlicerCaches
For Each SliCache In SliCaches
SliCName = SliCache.Name
For Each sliIt In SliCache.SlicerItems
A(1, UBound(A, 2)) = SliCName
A(2, UBound(A, 2)) = sliIt.Name
A(3, UBound(A, 2)) = sliIt.Selected
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Next sliIt
Next SliCache
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) - 1)
'Print it in a sheet
Sheets("Sheet1").Range("A1").Resize(UBound(A, 2), UBound(A, 1)).Value = Application.Transpose(A)
End Sub
与手动切片器缓存选择器相同:
Sub Save_Selected_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim SliCName As String
Dim sliIt As Excel.SlicerItem
Dim SaveSlice As Single
Dim A()
ReDim A(1 To 3, 1 To 1)
A(1, 1) = "Slicer Cache Name"
A(2, 1) = "Slicer Item Name"
A(3, 1) = "Selected"
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Set SliCaches = ActiveWorkbook.SlicerCaches
For Each SliCache In SliCaches
SliCName = SliCache.Name
SaveSlice = MsgBox("Do you want to save " & SliCName & " ?", vbYesNo, "Save slicers")
If SaveSlice <> vbYes Then
Else
For Each sliIt In SliCache.SlicerItems
A(1, UBound(A, 2)) = SliCName
A(2, UBound(A, 2)) = sliIt.Name
A(3, UBound(A, 2)) = sliIt.Selected
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Next sliIt
End If
Next SliCache
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) - 1)
'Print it in a sheet
Sheets("Sheet1").Range("A1").Resize(UBound(A, 2), UBound(A, 1)).Value = Application.Transpose(A)
End Sub
并加载:
Sub Load_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim sliIt As Excel.SlicerItem
Dim i As Double
Dim A()
'Load the array you printed
A = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp)).Value
Set SliCaches = ActiveWorkbook.SlicerCaches
For i = LBound(A, 1) To UBound(A, 1)
For Each SliCache In SliCaches
If SliCache.Name <> A(i, 1) Then
Else
For Each sliIt In SliCache.SlicerItems
If sliIt.Name <> A(i, 2) Then
Else
sliIt.Selected = A(i, 3)
End If
Next sliIt
End If
Next SliCache
Next i
End Sub
遍历 SlicerItems 或 PivotItems 真的很慢 - 我写了一个 post 检查瓶颈,我建议你看一看:
http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/
这是另一种方法,在大枢轴上 heck 会快很多。我们将您的原始数据透视表称为 ptOriginal。
- 复制 ptOriginal 并将其放置在隐藏 sheet 中。我们称它为 ptTemp
- 从中删除除感兴趣的字段之外的所有字段。我们称它为 pfTemp。
- 断开它与切片器的连接。
- 在该字段上添加一个新的切片器。我们称它为 slrTemp
当您想在以后恢复设置时,将 slrTemp 连接到 ptOriginal。
这行得通,因为 Excel 合理化了幕后的 SlicerCaches,并保留了您刚刚更改连接的设置。有关更多信息,请参阅我在 http://dailydoseofexcel.com/archives/2014/08/05/slicers-and-slicercaches/ 的文章。
我遇到了一个简单的 VBA 片段,用于检查 Excel 工作簿中所有活动切片器的切片器缓存。
Sub RetrieveSlicers()
Dim caches As Excel.SlicerCaches
Set caches = ActiveWorkbook.SlicerCaches
End Sub
通过在 End Sub
旁边放置一个断点,右键单击 cache
并选择 'Add Watch...'
(见下文)
您可以通过 'Watches' window 查看每个活动切片器中的所有项目。
我的问题是,我能否保存切片器缓存信息(特别是 SlicerItems)供以后使用(可能作为文本数组?),然后将保存的切片器缓存加载回切片器(使用保存的 SlicerItems 重新填充切片器) ?
示例如下:
我确信获取切片器缓存数据一样容易,设置切片器也同样容易缓存数据。
非常感谢您一如既往的帮助。
先生J
像这样的东西应该可以工作(我现在没有任何东西要测试):
Sub Save_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim SliCName As String
Dim sliIt As Excel.SlicerItem
Dim A()
ReDim A(1 To 3, 1 To 1)
A(1, 1) = "Slicer Cache Name"
A(2, 1) = "Slicer Item Name"
A(3, 1) = "Selected"
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Set SliCaches = ActiveWorkbook.SlicerCaches
For Each SliCache In SliCaches
SliCName = SliCache.Name
For Each sliIt In SliCache.SlicerItems
A(1, UBound(A, 2)) = SliCName
A(2, UBound(A, 2)) = sliIt.Name
A(3, UBound(A, 2)) = sliIt.Selected
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Next sliIt
Next SliCache
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) - 1)
'Print it in a sheet
Sheets("Sheet1").Range("A1").Resize(UBound(A, 2), UBound(A, 1)).Value = Application.Transpose(A)
End Sub
与手动切片器缓存选择器相同:
Sub Save_Selected_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim SliCName As String
Dim sliIt As Excel.SlicerItem
Dim SaveSlice As Single
Dim A()
ReDim A(1 To 3, 1 To 1)
A(1, 1) = "Slicer Cache Name"
A(2, 1) = "Slicer Item Name"
A(3, 1) = "Selected"
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Set SliCaches = ActiveWorkbook.SlicerCaches
For Each SliCache In SliCaches
SliCName = SliCache.Name
SaveSlice = MsgBox("Do you want to save " & SliCName & " ?", vbYesNo, "Save slicers")
If SaveSlice <> vbYes Then
Else
For Each sliIt In SliCache.SlicerItems
A(1, UBound(A, 2)) = SliCName
A(2, UBound(A, 2)) = sliIt.Name
A(3, UBound(A, 2)) = sliIt.Selected
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) + 1)
Next sliIt
End If
Next SliCache
ReDim Preserve A(LBound(A, 1) To UBound(A, 1), LBound(A, 2) To UBound(A, 2) - 1)
'Print it in a sheet
Sheets("Sheet1").Range("A1").Resize(UBound(A, 2), UBound(A, 1)).Value = Application.Transpose(A)
End Sub
并加载:
Sub Load_Slicers()
Dim SliCaches As Excel.SlicerCaches
Dim SliCache As Excel.SlicerCache
Dim sliIt As Excel.SlicerItem
Dim i As Double
Dim A()
'Load the array you printed
A = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp)).Value
Set SliCaches = ActiveWorkbook.SlicerCaches
For i = LBound(A, 1) To UBound(A, 1)
For Each SliCache In SliCaches
If SliCache.Name <> A(i, 1) Then
Else
For Each sliIt In SliCache.SlicerItems
If sliIt.Name <> A(i, 2) Then
Else
sliIt.Selected = A(i, 3)
End If
Next sliIt
End If
Next SliCache
Next i
End Sub
遍历 SlicerItems 或 PivotItems 真的很慢 - 我写了一个 post 检查瓶颈,我建议你看一看: http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/
这是另一种方法,在大枢轴上 heck 会快很多。我们将您的原始数据透视表称为 ptOriginal。
- 复制 ptOriginal 并将其放置在隐藏 sheet 中。我们称它为 ptTemp
- 从中删除除感兴趣的字段之外的所有字段。我们称它为 pfTemp。
- 断开它与切片器的连接。
- 在该字段上添加一个新的切片器。我们称它为 slrTemp
当您想在以后恢复设置时,将 slrTemp 连接到 ptOriginal。
这行得通,因为 Excel 合理化了幕后的 SlicerCaches,并保留了您刚刚更改连接的设置。有关更多信息,请参阅我在 http://dailydoseofexcel.com/archives/2014/08/05/slicers-and-slicercaches/ 的文章。