过滤后,VBA 在列表框中显示唯一项目

问题描述

我正在尝试找出在列表框中显示过滤范围而不重复特定列中的单元格的最佳方法。例如:

我有一个采购订单,其中可以有很多商品。我的用户有一个字段,他可以在其中搜索商品代码,而在我的列表框中,我只想显示一次采购订单编号。

我的数据库

My database

当我们搜索像“AA”这样的项目代码时,我的列表框显示采购订单 1 出现了两次,因为在这个条件中有两行输入。我希望能够为我过滤的任何列显示唯一的 PO。列 ITEM CODE 将被隐藏

过滤

Filtered

我应该使用哪种方法的任何建议? 我的完整表格有 70 列。

我正在使用高级过滤器进行过滤:

Private Sub FilterOrders()

Dim nRows As Long
Dim FilterRange As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Clear Listbox
Me.lbviewpo.RowSource = ""

'Clear all cells
shFilter.Cells.Clear

'Populate first row with the table headers
shOrders.ListObjects("TableOrders").HeaderRowRange.Copy shFilter.Range("A1")

If Me.TextItemCode.Value <> "" Then
    shFilter.Range("C2").Value = "*" & Me.TextItemCode.Value & "*"
Else
    shFilter.Range("C2").Clear
End If

'Copy and paste filtered cells and Paste copied cells in TableItemFilter
Set FilterRange = shOrders.Range("A1").CurrentRegion
FilterRange.AdvancedFilter Action:=xlFilterCopy,CriteriaRange:=shFilter.Range("A1:D2"),copytorange:=shFilter.Range("A4"),Unique:=True

'Count number of items filtered (including header)
nRows = shFilter.Range("A4").CurrentRegion.Rows.Count

'Load list of items in listbox if has data
If Not shFilter.Range("A5").Value = "" Then
    With Me.lbviewpo
        .ColumnCount = 4
        .ColumnHeads = True
        .ColumnWidths = "40,50,60"
        .RowSource = "=Filter!A5:D" & nRows + 3
    End With
End If

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...