Excel(VBA) - UserForm Combobox>列表框,停止重复

时间:2018-04-17 12:32:42

标签: excel vba excel-vba

从组合框中选择一个类别后,列表框会更新,只显示与组合框选择相关的记录。然而,该列表正在产生重复,我想知道如何防止这种情况发生。

Private Sub ProdComp_Change()
Dim RowMax As Integer
Dim ws As Worksheet
Dim countexit As Integer
Dim cellcombo2 As String
Dim i As Integer

Set ws = ThisWorkbook.Sheets("products")
RowMax = ws.Cells(Rows.Count, "B").End(xlUp).Row

Me.LBType.Clear

With LBType
    For i = 2 To RowMax
        If ws.Cells(i, "B").Value = ProdComp.Text Then
        .AddItem ws.Cells(i, "c").Value
        Else
        End If
    Next i
End With

End Sub

Worksheet View

UserForm View

2 个答案:

答案 0 :(得分:1)

尝试将项目添加到唯一的集合,然后将该集合添加到列表框中。这样你就不会得到任何重复。

试试这个

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: 'index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'index_bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin()
  ]
}

如果你有太多的数据,那么你可以将数据复制到数组而不是循环遍历行,然后创建唯一的集合。

enter image description here

答案 1 :(得分:0)

你可以尝试一下......

Private Sub ProdComp_Change()
Dim RowMax As Integer
Dim ws As Worksheet
Dim countexit As Integer
Dim cellcombo2 As String
Dim i As Integer
Dim dict

Set ws = ThisWorkbook.Sheets("products")
RowMax = ws.Cells(Rows.Count, "B").End(xlUp).Row
Set dict = CreateObject("Scripting.Dictionary")

Me.LBType.Clear

With LBType
    For i = 2 To RowMax
        If ws.Cells(i, "B").Value = ProdComp.Text Then
            dict.Item(ws.Cells(i, "c").Value) = ""
        End If
    Next i
    If dict.Count > 0 Then .List = dict.keys
End With
End Sub
相关问题