2016-07-22 54 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

ILL嘗試此操作。謝謝 :-) –