2013-05-03 71 views
0

我想從數據透視表字段加載列表框中的行。我發現下面的代碼適用於ActiveX控件列表框,但不適用於用戶窗體列表框。 UserForm控件收到一個438錯誤。我正在處理一系列用戶表單,並且只能將ActiveX控件嵌入到工作表中。用戶自定義列表框填充數據透視表值 - excel

Private Sub ListBox1_Click() 
     Dim Pf As PivotField 
     Dim I As Integer 
     Set Pf = Worksheets("Sheet4").PivotTables(1).PivotFields("Field Name") 
     With ActiveSheet.ListBox1 
     .Clear 
      For I = 1 To Pf.PivotItems.Count 
      .AddItem Pf.PivotItems(I) 
      Next 
     End With 
    End Sub 

原代碼發現這裏:http://www.pcreview.co.uk/forums/fill-listbox-values-pivot-table-field-example-t967653.html

在此先感謝您的幫助!在LinkedIn

回答

1

有人能夠回答這個問題,我就關機會張貼這也可以幫助別人,將來別人:

Private Sub UserForm_Initialize() 

    Dim pvtTable As PivotTable 
    Dim pvtField As PivotField 
    Dim lngIndex As Long 

    Set pvtTable = Worksheets("Sheet4").PivotTables(1) 
    Set pvtField = pvtTable.PivotFields("Field Name") 

    For lngIndex = 1 To pvtField.PivotItems.Count 
    UserForm1.ListBox1.AddItem pvtField.PivotItems(lngIndex).Name 
    Next 

    End Sub 

確保參考Userform1您的用戶窗體的名稱相匹配。或者使用ME關鍵字。