2017-11-14 2 views
1

VBA의 사용자 폼에 피벗 테이블을 삽입 할 수 있습니까? 나는 이것에 대해 other question을 보았지만 오른쪽 클릭 메뉴에서 Microsoft Office PivotTable control을 찾을 수 있습니다. 나는 Tree View를 찾았지만 생각하지 않은 것은 그다지 똑같은 것은 아니다.사용자 폼에서 피벗 테이블을 사용할 수 있습니까?

내 사무실에서 사용하기위한 인벤토리 통합 문서를 만드는거야

UPDATE. 다른 사람들이 그것을 사용하여 우리가 가지고있는 것을보고 재고 목록에있는 품목을 요청할 수있게 할 것입니다. 이 경우 Userform을 사용하겠습니다. 이미 여러 피벗 테이블이있는 통합 문서의 대시 보드가 있습니다. Userform에 2 개를 사용하고 싶습니다.

일반 사용자는 통합 문서 편집에 액세스 할 수 없으며 어떤 시트가 표시되어 있는지 변경하기 위해 액세스 권한이 있어야이 번호를 Userform에 추가하려는 2 개의 피벗을 볼 수 있습니다.

따라서 최종 사용자는 인벤토리에있는 항목을보고 요청하거나 요청할 PO를 작성하는 전자 메일을 보낼 수있는 피벗 테이블을 갖게됩니다.

+0

그 질문은 실제로 오래되었습니다. 사용중인 Office 버전은 무엇입니까? 사무실 (2016)에서,이 제어는 더 이상 이용 가능하지 않다. –

+0

Office 2016 – Mike

+0

흥미로운 질문입니다. 사용자 정의 폼 (UserForm)에서 그리드가 아닌 사용자가 원하는 이유에 대해 자세히 설명해 주시겠습니까?이 기능이 추가 될지 궁금합니다. – jeffreyweir

답변

1

저는 오랫동안 Excel을 사용해 왔지만이 조합 (UserForm + PT)이 필요한 사람은 들어 본 적이 없지만 어쨌든 빠른 Google 검색을 수행했습니다.

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

관련 문제