vba 填充数据透视表值的用户表单列表框 - excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16365200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:58:52  来源:igfitidea点击:

Userform listbox filled with pivot table values - excel

vbalistboxexcel-2010pivot-tableuserform

提问by user1946481

I am trying to load a listbox with the rows from a pivot table field. I have found the following code that works for a ActiveX Control Listbox, but not for a UserForm Listbox. The UserForm control receives a 438 error. I am working with a series of UserForms and the activeX control can only be embedded in a worksheet.

我正在尝试使用数据透视表字段中的行加载列表框。我发现以下代码适用于 ActiveX 控件列表框,但不适用于用户窗体列表框。UserForm 控件收到 438 错误。我正在使用一系列用户窗体,而 activeX 控件只能嵌入到工作表中。

   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

The original code was found here: http://www.pcreview.co.uk/forums/fill-listbox-values-pivot-table-field-example-t967653.html

原始代码在这里找到:http: //www.pcreview.co.uk/forums/fill-listbox-values-pivot-table-field-example-t967653.html

Thanks in advance for the help!

在此先感谢您的帮助!

回答by user1946481

Someone on LinkedIn was able to answer the question, I am posting this on the off chance it helps someone else in the future:

LinkedIn 上的某个人能够回答这个问题,我发布此信息是为了将来可以帮助其他人:

   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 

Make sure the reference Userform1 matches the name of your userform. Or use the ME keyword instead.

确保参考 Userform1 与您的用户表单名称匹配。或者改用 ME 关键字。