0

我对 VBA 和 Excel 宏完全陌生,我想做的就是:

我希望一旦我单击切片器中的数据,它就会自动复制到剪贴板中。

我的切片器名称是:Slicer_Internal_Punter_ID

如果您仍需要其他信息,请告诉我。我以为这很容易,但我现在要疯了。

谢谢。我很乐意欣赏它。

这是切片机

这是切片器的详细信息

在此处输入图像描述.

4

1 回答 1

1

您可以使用以下过程和事件来获取所选项目的名称。

将此过程添加到任何模块并使用即时窗口(查看/即时窗口)来读取和复制 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

第二个片段是必须添加到受切片器影响的工作表的工作表事件。

在宏编辑器(项目资源管理器窗口)中双击工作表以添加代码: 将事件添加到工作表

第二个片段会将所选项目打印到即时窗口。

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")

此外,要将项目名称发布到剪贴板,您可以查看以下链接:如何将文本复制到剪贴板

于 2016-07-22T15:36:17.357 回答