다음 절차 및 이벤트를 사용하여 선택한 항목의 이름을 가져올 수 있습니다.
이 절차를 모든 모듈에 추가하고 직접 실행 창 (보기/직접 실행 창)을 사용하여 SliceCache의 올바른 이름을 읽고 복사하십시오. 다른 스 니펫에서 사용해야합니다.
Sub GetSlicerData()
'## Variables ##
Dim iSlicerCache As SlicerCache 'Slicer Cache Object
Dim iSlicerItem As SlicerItem 'Slicer Item
Dim iSlicer As Slicer 'Slicer Object
'## Looping through Slicer Caches in 'ThisWorkbook' ##
For Each slSlicerCache In ThisWorkbook.SlicerCaches
Debug.Print ("Slicer Cache Namee: " & slSlicerCache.Name) 'Printing the name property of the SlicerCaches
'## Looping through Slicers contained in the SlicerCaches ##
For Each iSlicer In slSlicerCache.Slicers
Debug.Print ("Slicer Name: " & iSlicer.Name) 'Printing the slicer names
Next iSlicer
'## Looping through Items contained in the SlicerCaches ##
'and testing selection status
For Each iSlicerItem In slSlicerCache.SlicerItems
If iSlicerItem.Selected = True Then Debug.Print ("Selected Item: " & iSlicerItem.Name) 'Printing the slicer items
Next iSlicerItem
Next slSlicerCache
End Sub
번째 단편은 슬라이서의 영향 시트에 추가되어있는 워크 이벤트이다. 매크로 편집기 (프로젝트 탐색기 창)에서 시트에
더블 클릭 코드를 추가합니다 : 이 Adding the Event to the sheet
두 번째 조각은 즉시 창에 선택한 항목을 인쇄합니다.
Private Sub Worksheet_Change(ByVal Target As Range)
'Debug.Print ("Sheet name: " & Me.Name) 'Sheet name
'## Looping through Slicer Caches in 'ThisWorkbook' ##
Dim slcSlicerCache As SlicerCache 'Slicer Cache Object
Dim iSlicerItem As SlicerItem 'Slicer Item
Dim stItems As String
'## Setting slicer cache ##
'Replace with the correct name: "Slicer_Internal_Punter_ID"
Set slcSlicerCache = ThisWorkbook.SlicerCaches("Slicer_Internal_Punter_ID")
For Each iSlicerItem In slcSlicerCache.SlicerItems
If iSlicerItem.Selected = True Then
Debug.Print ("Selected Item: " & iSlicerItem.Name) 'Printing selected
If Len(stItems) = 0 Then stItems = iSlicerItem.Name Else stItems = stItems & vbNewLine & iSlicerItem.Name
End If
Next iSlicerItem
Debug.Print ("Selected Items: " & vbNewLine & stItems) 'Printing selected
'ADD CODE HERE: moving content to clipboard
End Sub
이 줄 슬라이서의 정확한 이름을 사용하도록주의하십시오 :
또한
Set slcSlicerCache = ThisWorkbook.SlicerCaches("Slicer_Internal_Punter_ID")
다음 링크를 확인하실 수 있습니다 클립 보드에 항목 이름을 게시 할 수 : How to copy text to clipboard
이 사이트를 무료 코딩 서비스가 아닙니다. VBA를 처음 사용하는 경우 덜 복잡한 것으로 시작하는 것이 좋습니다. – teylyn
그래도 여전히 고마워. –