2016-07-22 3 views
0

나는 VBA 및 Excel 매크로 완전히 새로 온 사람과 내가하고 싶은 단순히 이것이다 :VBA 슬라이서 복사 방법

내가 슬라이서에서 데이터를 클릭하는 즉시, 그것은 자동으로 복사됩니다 원하는

클립 보드에.

내 슬라이서 이름은 : 당신은 여전히 ​​추가 정보를 필요로하는 경우, 알려 주시기 바랍니다

을 Slicer_Internal_Punter_ID. 나는 그것이 쉽다라고 생각했다. 그러나 나는 지금 미쳤 게되고있다.

감사합니다. 기꺼이 감사하겠습니다. 여기

This is the slicer

는 슬라이서 세부

enter image description here입니다.

+2

이 사이트를 무료 코딩 서비스가 아닙니다. VBA를 처음 사용하는 경우 덜 복잡한 것으로 시작하는 것이 좋습니다. – teylyn

+0

그래도 여전히 고마워. –

답변

0

다음 절차 및 이벤트를 사용하여 선택한 항목의 이름을 가져올 수 있습니다.

이 절차를 모든 모듈에 추가하고 직접 실행 창 (보기/직접 실행 창)을 사용하여 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

+0

통역이 내용을 확인하십시오. 감사 :-) –