【问题标题】:Using advance filter to populate listbox in userform使用高级过滤器填充用户表单中的列表框
【发布时间】:2018-09-24 00:03:57
【问题描述】:

我有一个主表,其中 A 列包含“客户名称”,B 列包含“频率”,其中必须将报告发送给这些客户。客户端名称重复,如下面的屏幕截图所示

我的用户表单有一个组合框,它将从 B 列获取频率详细信息,以及一个列表框,它将从 A 列填充数据,即唯一的客户名称。

经过一些研究,我可以使用高级过滤器选项使用以下代码填充客户列表框数据。

Dim arrUnqItems As Variant

        With Sheets("Data")

            .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).AdvancedFilter xlFilterCopy, , .Cells(1, .Columns.Count), True
            arrUnqItems = Application.Transpose(.Range(.Cells(2, .Columns.Count), .Cells(.Rows.Count, .Columns.Count).End(xlUp)).Value)
            .Columns(.Columns.Count).Clear

        End With

        Me.custList.Clear
        Me.custList.List = arrUnqItems

        Erase arrUnqItems

但此列表框代码不是基于组合框选择过滤值,而只是显示唯一的客户名称。有人可以帮忙吗?根据对组合框和列表框的选择,我将搜索数据库以获取客户的其他详细信息并填充到表单的其他部分。

【问题讨论】:

  • 可以发截图吗?很难想象你在做什么。
  • 已添加屏幕截图。

标签: vba excel


【解决方案1】:

您必须为CriteriaRange 字段写下一个范围:

    Dim freq As String
    freq = Combobox1.Value ' get the currently selected frequency (change "Combobox1" to your actual frequency combobox name)
    With Sheets("Data")
        .Cells(1, .Columns.Count - 2).Resize(2) = Application.Transpose(Array("Frequency", freq )) 'write the "CriteriaRange" in the third last column
        .Range("B1", .Cells(.Rows.Count, "A").End(xlUp)).AdvancedFilter xlFilterCopy, .Cells(1, .Columns.Count - 2).Resize(2), .Cells(1, .Columns.Count - 1), True ' filter adopting the written "CriteriaRange" and paste filtered values from the second last column (two columns will be pasted: "Customer Name" and "Frequency")
        arrUnqItems = Application.Transpose(.Range(.Cells(2, .Columns.Count - 1), .Cells(.Rows.Count, .Columns.Count - 1).End(xlUp)).Value) ' fill array of unique customer names from the second last column
        .Columns(.Columns.Count - 2).Resize(, 3).Clear
    End With

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 2013-02-26
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多