将切片器值保存到工作表

时间:2014-06-20 09:42:03

标签: excel excel-vba pivot-table vba

嗨我很难尝试在切片机中循环以查看它们是否已过滤。

我的目标是将所有选定的切片器放入工作表中,以便我可以将一个(从高到低)过滤器应用于基础数据透视数据,以便我可以选择超过预算的前五名"基于数据切片器中的选择。

我有以下代码,但看到错误:Run Time Error 438’ Object doesn’t support this method

有人可以告诉我如何实现这一目标。

Public Sub top_over_under_booked()
    Dim oSi As SlicerItem
    Dim oSlicercache As SlicerCache
    Dim oSl As SlicerCacheLevel
    Dim oPt As PivotTable
    Dim oSh As Worksheet
    Set target_ws = ThisWorkbook.Worksheets("Get Slicer Selections")
    For Each oSlicercache In ThisWorkbook.SlicerCaches

        For Each oPt In oSlicercache.PivotTables

            oPt.Parent.Activate     'Slice Name
            worksheet_name = UCase(oPt.Parent.Name)
            If worksheet_name = UCase("Chart Analysis 5 Years") Then  
                column_no = 0
                slicer_name = UCase(oSlicercache.Name)
                Select Case UCase(oSlicercache.Name)
                    Case Is = "SLICER_FY1"
                        column_no = 1
                    Case Is = "SLicer_REPORT_PT_DEPT1"
                        column_no = 2
                    'There are actually loads more slicer which needs to ne ignored.
                End Select
                If column_no <> 0 Then
                    For Each oSl In ActiveWorkbook.SlicerCaches(oSlicercache.Name) ' <----- Error
                        For Each oSi In oSl.SlicerItems
                            'oSi.Selected = True
                            check_slicer_string = oSi.Value
                            'target_ws.Cells(ource_ws.Cells(65000, column_no).End(xlUp).Row + 1, column_no) = oSlicercache.Value
                        Next
                    Next
                End If
                oPT.Parent.Name
            End If
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

有两种不同的Excel

使用Microsoft.Office.Tools.Excel; 使用Excel = Microsoft.Office.Interop.Excel;

ThisWorkBook.cs中的示例

    public string SelectedReportCategories()
    {
        var selection = "";

        var myTarget = this;

        try
        {
            var caches = myTarget.SlicerCaches["Slicer_REPORT"];

            foreach (Excel.SlicerItem slicerItem in caches.SlicerItems)
            {
                if (slicerItem.Selected)
                    selection = selection + " " + slicerItem.Name;
            }
        }
        catch (Exception e)
        {
            ShowError("Configuration", (e.InnerException != null) ? e.InnerException.Message : e.Message);
        }

        return selection;
    }