从 Vba 中的数据透视表检索数据

发布于 2024-10-19 07:47:28 字数 128 浏览 2 评论 0原文

我有一个 Excel 工作表,其中包含三个数据透视表,数据透视表名称分别为数据透视表1...数据透视表3,活动字段名称分别为国家/地区、语言和打印机。我需要的是将每个数据透视表中的所有数据获取到每个字符串或字符串数​​组。任何帮助将非常感激。

I have an excel work sheet which contains three pivot tables having Pivot Table Names as PivotTable1...PivotTable3 and Active field names as Country, Language and Printers respectively. What I need is to get all data in each pivot table to each string or string array. Any help will be very thankful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-10-26 07:47:28

快速&脏的让你继续前进;数据透视表的所有单元格位于一个线性字符串中,以“;”分隔。这应该会给使用哪些方法和属性带来足够的启发。请注意:Tmp 无法容纳无限大的数据透视表,如果它们变得非常大,请考虑将 Tmp 写入文件。

Sub PTTest()
Dim SH As Worksheet ' the current worksheet from the colection of workbooks
Dim PT As PivotTable ' the current pivot table from the current worksheet
Dim PTC As Range  ' the cell range of the current pivot table
Dim Tmp As String ' the buffer for concatenated cell values

    Tmp = ""
    ' process all sheets, as Pivot table objects are contained by sheets
    For Each SH In ActiveWorkbook.Worksheets

        For Each PT In SH.PivotTables

            For Each PTC In PT.TableRange1.Cells
                ' all cells in one buffer, seperated by ";"
                ' if you want to include page header cells, use
                ' "PT.TableRange2.Cells" instead
                Tmp = Tmp & PTC & ";"
            Next PTC

            ' *** do something *** with the buffer
            ' ok very simple we print it into the debugger's Immediate window
            Debug.Print Tmp

            ' empty buffer for next pivot table
            Tmp = ""

        Next PT
    Next SH
End Sub

希望有帮助......祝你好运 MikeD

A quick & dirty one to get you going; all cells of a Pivot table in one linear string, seperated by ";". This should give enough inspiration on which methods and properties to use. Beware: Tmp cannot hold indefinitely large pivot tables, if they grow enormously large, consider writing Tmp to a file instead.

Sub PTTest()
Dim SH As Worksheet ' the current worksheet from the colection of workbooks
Dim PT As PivotTable ' the current pivot table from the current worksheet
Dim PTC As Range  ' the cell range of the current pivot table
Dim Tmp As String ' the buffer for concatenated cell values

    Tmp = ""
    ' process all sheets, as Pivot table objects are contained by sheets
    For Each SH In ActiveWorkbook.Worksheets

        For Each PT In SH.PivotTables

            For Each PTC In PT.TableRange1.Cells
                ' all cells in one buffer, seperated by ";"
                ' if you want to include page header cells, use
                ' "PT.TableRange2.Cells" instead
                Tmp = Tmp & PTC & ";"
            Next PTC

            ' *** do something *** with the buffer
            ' ok very simple we print it into the debugger's Immediate window
            Debug.Print Tmp

            ' empty buffer for next pivot table
            Tmp = ""

        Next PT
    Next SH
End Sub

Hope that helps .... good luck MikeD

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文