Excel VBA – 检查报告切片器select(如果selectALL,则跳过)

我需要一些VBA代码的帮助。 我有一个AgeRange切片机,我有一个工作脚本插入一行,添加一个时间戳,然后报告切片机的select。

我想添加一些东西,这将跳过如果切片机中的所有项目被选中(真)的过程。

有什么我可以插入,说:“如果切片机没有被触摸(所有项目是真的),然后结束小组”。

以下是我到目前为止的代码:

Dim cache As Excel.SlicerCache Set cache = ActiveWorkbook.SlicerCaches("Slicer_AgeRange") Dim sItem As Excel.SlicerItem For Each sItem In cache.SlicerItems If sItem.Selected = True Then xAge = xAge & sItem.Name & ", " Next sItem Rows("1:1").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A1").Select ActiveCell.FormulaR1C1 = Format(Now(), "MM-DD-YYYY HH:MM AM/PM") Range("B1").Select ActiveCell.FormulaR1C1 = xAge Range("C1").Select End Sub 

任何帮助是极大的赞赏!

这比你所要求的要多一点,但我想我会分享,因为我只是写这个供自己使用。 它清除物理上位于工作表上的所有切片,只要它们被过滤(并非全部选中)。 对于你的问题,好的是每个项目循环。 并在其后的线。

 Sub clearWorksheetSlicers(ws As Worksheet) 'clears all slicers that have their shape on a specific worksheet Dim slSlicer As Slicer Dim slCache As SlicerCache Dim item As SlicerItem Dim hasUnSel As Boolean For Each slCache In ThisWorkbook.SlicerCaches For Each slSlicer In slCache.Slicers If slSlicer.Shape.Parent Is ws Then For Each item In slCache.SlicerItems If item.Selected = False Then hasUnSel = True Exit For End If Next item If hasUnSel = True Then slCache.ClearManualFilter hasUnSel = False End If Next slSlicer Next slCache End Sub 

NVM。 我自己做到了 🙂

 Dim cache As Excel.SlicerCache Dim sName As Slicers Dim sItem As Excel.SlicerItem Dim xSlice As String Dim xName As String For Each cache In ActiveWorkbook.SlicerCaches xName = StrConv(Replace(cache.Name, "AgeRange", "Ages") xCheck = 0 For Each sItem In cache.SlicerItems If sItem.Selected = False Then xCheck = xCheck + 1 Else xCheck = xCheck End If Next sItem If xCheck > 0 Then For Each sItem In cache.SlicerItems If sItem.Selected = True Then xSlice = xSlice & sItem.Caption & ", " End If Next sItem Rows("1:1").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("B1").Select ActiveCell.FormulaR1C1 = xName & ": " & xSlice xSlice = "" End If Next cache Range("A1").Select ActiveCell.FormulaR1C1 = Format(Now(), "MM-DD-YYYY HH:MM AM/PM") End Sub