2017-09-03 34 views
0

사용자 선택에 따라 슬라이서에서 요소를 숨기거나 표시하려면 Excel VBA를 사용하고 있습니다. A, B 등은 슬라이서의 요소 이름 어디VBA로 슬라이서 캐시를 업데이트하는 방법

Private Sub removeFilterWithSlicer() 

Dim slicerCache As slicerCache 

Set slicerCache = ThisWorkbook.SlicerCaches("Slicer_Channel1") 

slicerCache.SlicerItems("A").Selected = False 
slicerCache.SlicerItems("B").Selected = False 
slicerCache.SlicerItems("C").Selected = False 
slicerCache.SlicerItems("D").Selected = False 
slicerCache.SlicerItems("E").Selected = False 
slicerCache.SlicerItems("F").Selected = False 

End Sub 

:

I는 다음의 코드를 갖는다. 슬라이서 캐시 (Slicer_Channel1)의 이름을 확인했습니다. 문제는 요소가 예상대로 선택 취소되지 않는다는 것입니다. 코드를 디버깅 할 때 각 요소가 하나씩 선택 해제되지만 프로 시저 끝, 즉 End Sub에 도달하면 모든 요소가 선택된 상태로 돌아갑니다.

모든 포인터?

+0

정확히 무엇을하려고 할 수 있습니까? 코드에서 슬라이서를 지우고 싶습니까? 아니면 특정 SlicerItem에서 슬라이서를 걸러 내길 원합니 까? – jeffreyweir

+0

@jeffreyweir 기본적으로이 코드를 사용하여 슬라이서를 지우고 사용자 이벤트를 선택하지만 jsotola에서 제안한대로 모든 필터를 마우스로 선택 취소 할 수는 없습니다. 제안을 위해 아직 열어 두십시오. – hbabbar

+0

차갑습니다. 이 작업을 수행 할 수 있습니다. 하지만 먼저 사용자가 스프레드 시트와 상호 작용하는 방식과 스프레드 시트를 실행할 때 어떤 일을하고 싶은지 조금 더 자세히 설명 할 수 있습니까? Slicer를 프로그래밍 방식으로 지우는 것보다는 단순히 사용자가 Slicer 자체를 사용하기를 원하지 않는 이유를 알고 있으면 편리 할 것입니다. – jeffreyweir

답변

0

이 코드는 vSelection이라는 배열에서 슬라이서를 필터링하는 방법을 보여줍니다.

Option Explicit 

Sub FilterSlicer() 
Dim slr As Slicer 
Dim sc As SlicerCache 
Dim si As SlicerItem 
Dim i As Long 
Dim vItem As Variant 
Dim vSelection As Variant 

Set sc = ActiveWorkbook.SlicerCaches("Slicer_ID") 
'Set sc = slr.SlicerCache 

vSelection = Array("B", "C", "E") 

For Each pt In sc.PivotTables 
    pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed 
Next pt 

With sc 

    'At least one item must remain visible in the Slicer at all times, so make the first 
    'item visible, and at the end of the routine, check if it actually *should* be visible 
    .SlicerItems(1).Selected = True 

    'Hide any other items that aren't already hidden. 
    'Note that it is far quicker to check the status than to change it. 
    ' So only hide each item if it isn't already hidden 
    For i = 2 To .SlicerItems.Count 
     If .SlicerItems(i).Selected Then .SlicerItems(i).Selected = False 
    Next i 

    'Make the PivotItems of interest visible 
    On Error Resume Next 'In case one of the items isn't found 
    For Each vItem In vSelection 
     .SlicerItems(vItem).Selected = True 
    Next vItem 
    On Error GoTo 0 

    'Hide the first PivotItem, unless it is one of the countries of interest 
    On Error Resume Next 
    If InStr(UCase(Join(vSelection, "|")), UCase(.SlicerItems(1).Name)) = 0 Then .SlicerItems(1).Selected = False 
    If Err.Number <> 0 Then 
     .ClearAllFilters 
     MsgBox Title:="No Items Found", Prompt:="None of the desired items was found in the Slicer, so I have cleared the filter" 
    End If 
    On Error GoTo 0 
End With 


For Each pt In sc.PivotTables 
    pt.ManualUpdate = False 
Next pt 

End Sub 
0

하나 이상을 선택해야합니다. 마우스로 선택하는 경우와 동일한 방식으로 작동합니다. 모두 선택 해제 할 수 없음