如何从数据透视表中填充用户表单列表?

时间:2016-06-10 18:04:31

标签: listboxitem

我需要在User表单上填充数据透视表,并且只使用一个字段就成功了,但我需要显示4个字段。这就是我所拥有但却不断出错:

Private Sub UserForm_Initialize()

Dim pvtTable As PivotTable
   Dim pvtDeliv, pvtDoc, pvtName, pvtMaterial As PivotField
   Dim lngIndex As Long
   Dim arrData() As String

 Set pvtTable = Worksheets("orders").PivotTables("orders")
   Set pvtDoc = pvtTable.PivotFields("OriginDoc.")
   Set pvtDeliv = pvtTable.PivotFields("Deliv.Date")
   Set pvtMaterial = pvtTable.PivotFields("Material")
   Set pvtName = pvtTable.PivotFields("Name")

    ListBox1.ColumnCount = 4

    For lngIndex = 1 To pvtDeliv.PivotItems.Count
    UserForm1.ListBox1.AddItem pvtDeliv.PivotItems(lngIndex).Name.Value
    ListBox1.List(lngIndex, 2) = pvtMaterial.PivotItems(lngIndex).Name.Value
    ListBox1.List(lngIndex, 3) = pvtName.PivotItems(lngIndex).Name.Value
    ListBox1.List(lngIndex, 4) = pvtDoc.PivotItems(lngIndex).Name.Value

    Next


  End Sub

这是我试图填充的数据透视表

Pivot table

提前谢谢。 Martyna

1 个答案:

答案 0 :(得分:0)

经过一番调查后,我发现了这一点,这是经过修改的代码:

Private Sub UserForm_Initialize()

Dim pvtTable As PivotTable
   Dim pvtDeliv, pvtDoc, pvtName, pvtMaterial As PivotField
   Dim lngIndex As Long
   Dim arrData() As String

   Set pvtTable = Worksheets("orders").PivotTables("orders")
   Set pvtDoc = pvtTable.PivotFields("OriginDoc.")
   Set pvtDeliv = pvtTable.PivotFields("Deliv.Date")
   Set pvtMaterial = pvtTable.PivotFields("Material")
   Set pvtName = pvtTable.PivotFields("Name")

    ListBox1.ColumnCount = 4

    For lngIndex = 1 To pvtDeliv.PivotItems.Count
    UserForm1.ListBox1.AddItem
    ListBox1.List(lngIndex - 1, 0) = pvtDeliv.PivotItems(lngIndex).Name
    ListBox1.List(lngIndex - 1, 1) = pvtMaterial.PivotItems(lngIndex).Name
    ListBox1.List(lngIndex - 1, 2) = pvtName.PivotItems(lngIndex).Name
    ListBox1.List(lngIndex - 1, 3) = pvtDoc.PivotItems(lngIndex).Name

    Next


  End Sub