是否可以在用户表单中使用数据透视表?

时间:2017-11-14 13:21:59

标签: excel vba excel-vba pivot-table

是否可以将数据透视表插入VBA中的用户窗体?我看到了这个other question,但我可以在右键菜单中找到Microsoft Office PivotTable control。我确实找到了Tree View,但这并不是我想不到的。

更新

我正在创建一个供我办公室使用的库存工作簿。我会允许其他人使用它来查看我们拥有的内容并请求我们在库存中拥有的商品。我将使用Userform来实现此目的。我在工作簿中有一个仪表板,它已经有几个数据透视表。我想在Userform中使用2个。

普通用户无权修改工作簿,甚至无法更改正在显示的工作表,他们只需要访问权限即可查看我要添加到此Userform的2个支点。

因此,最终结果将是最终用户将拥有一个数据透视表,允许他们查看我们在库存中的内容并请求它,或者发送一封电子邮件来创建订购它的订单。

1 个答案:

答案 0 :(得分:1)

我一直在使用Excel很长一段时间,我从来没有听说过任何人需要这种组合(UserForm + PT),但无论如何,我做了一个快速的谷歌搜索并想出了这个。

Option Explicit

Dim cnnConnection As Object

Private Sub Form_Load()

   Dim strProvider As String
   Dim view As PivotView
   Dim fsets As PivotFieldSets
   Dim c As Object
   Dim newtotal As PivotTotal

   strProvider = "Microsoft.Jet.OLEDB.4.0"
   ' Create an ADO object
   Set cnnConnection = CreateObject("ADODB.Connection")
   ' Set the provider and open the connection to the database
   cnnConnection.Provider = strProvider
   cnnConnection.Open "C:\pivottest.mdb"
   ' Set the pivot table's connection string to the cnnConnection's connection string
   PivotTable1.ConnectionString = cnnConnection.ConnectionString
   ' SQL statement to get everything from table1
   PivotTable1.CommandText = "Select * from table1"

   ' Get variables from the pivot table
   Set view = PivotTable1.ActiveView
   Set fsets = PivotTable1.ActiveView.FieldSets
   Set c = PivotTable1.Constants

   ' Add Category to the Row axis and Item to the Column axis
   view.RowAxis.InsertFieldSet fsets("Category")
   view.ColumnAxis.InsertFieldSet fsets("Item")

   ' Add a new total - Sum of Price
   Set newtotal = view.AddTotal("Sum of Price", view.FieldSets("Price").Fields(0), c.plFunctionSum)
   view.DataAxis.InsertTotal newtotal
   view.DataAxis.InsertFieldSet view.FieldSets("Price")

   ' Set some visual properties
   PivotTable1.DisplayExpandIndicator = False
   PivotTable1.DisplayFieldList = False
   PivotTable1.AllowDetails = False
End Sub

Private Sub Form_Terminate()
  ' Remove reference to the ADO object
  Set cnnConnection = Nothing
End Sub


Private Sub PivotTable1_DblClick()
   Dim sel As Object
   Dim pivotagg As PivotAggregate
   Dim sTotal As String
   Dim sColName As String
   Dim sRowName As String

   Dim sMsg As String

   ' Get the selection object you double-clicked on
   Set sel = PivotTable1.Selection
   ' If it is a aggregate, you can find information about it
   If TypeName(sel) = "PivotAggregates" Then
      ' Select the first item

      Set pivotagg = sel.Item(0)
      ' Display the value
      MsgBox "The cell you double-clicked has a value of '" & pivotagg.Value & "'.", vbInformation, "Value of Cell"

      ' Get variables from the cell
      sTotal = pivotagg.Total.Caption
      sColName = pivotagg.Cell.ColumnMember.Caption
      sRowName = pivotagg.Cell.RowMember.Caption

      ' Display the row and column name
      sMsg = "The value is " & sTotal & " by " & sRowName & " by " & sColName
      MsgBox sMsg, vbInformation, "Value Info"
   End If
End Sub

看看您是否可以根据具体设置调整该概念。

https://support.microsoft.com/en-us/help/235542/how-to-use-the-pivottable-office-web-component-with-vb