VBA删除ListBox重复项

时间:2016-08-24 17:27:46

标签: vba excel-vba duplicates listboxitem excel

我试图从另一个有重复项的工作表中添加名称列表。在列表框中,我希望有唯一的名称,而不是重复的名称。以下代码没有为重复项排序它,它出错了。任何帮助表示赞赏。

Dim intCount As Integer
Dim rngData As Range
Dim strID As String
Dim rngCell As Range
dim ctrlListNames as MSForms.ListBox
Set rngData = Application.ThisWorkbook.Worksheets("Names").Range("A").CurrentRegion

'declare header of strID and sort it
strID = "Salesperson"
rngData.Sort key1:=strID, Header:=xlYes
'Loop to add the salesperson name and to make sure no duplicates are added
For Each rngCell In rngData.Columns(2).Cells
    If rngCell.Value <> strID Then
        ctrlListNames.AddItem rngCell.Value
        strID = rngCell.Value
    End If
Next rngCell

3 个答案:

答案 0 :(得分:2)

方式1

使用此选项删除重复项

Sub Sample()
    RemovelstDuplicates ctrlListNames
End Sub

Public Sub RemovelstDuplicates(lst As msforms.ListBox)
    Dim i As Long, j As Long
    With lst
        For i = 0 To .ListCount - 1
            For j = .ListCount - 1 To (i + 1) Step -1
                If .List(j) = .List(i) Then
                    .RemoveItem j
                End If
            Next
        Next
    End With
End Sub

方式2

创建一个唯一的集合,然后将其添加到列表框

Dim Col As New Collection, itm As Variant

For Each rngCell In rngData.Columns(2).Cells
    On Error Resume Next
    Col.Add rngCell.Value, CStr(rngCell.Value)
    On Error GoTo 0
Next rngCell

For Each itm In Col
    ctrlListNames.AddItem itm
Next itm

答案 1 :(得分:0)

Private Sub Workbook_Open()
Dim ctrlListNames As MSForms.ListBox
Dim i As Long
Dim j As Long

ctrlListNames.List = Application.ThisWorkbook.Worksheets("Names").Range("Salesperson").Value


With ctrlListNames
For i = 0 To .ListCount - 1
    For j = .ListCount To (i + 1) Step -1
        If .List(j) = .List(i) Then
            .RemoveItem j
        End If
    Next
Next
End With


End Sub

它表示无效的属性数组索引。

答案 2 :(得分:0)

它表示无效的属性数组索引,因为删除条目后列表会缩短。如果使用FOR,则最终值是静态的,因此,我们需要使用DO while循环。使用以下代码删除重复项。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-head-fixed text-nowrap" id="myTable">
  <thead>
    <tr>
      <th>Qty</th>
      <th>Item</th>
      <th>Description</th>
      <th>Unit Price</th>
      <th>Tax</th>
      <th>Total</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr class="main_data">
      <td><input type="number" name="items[0][inv_quantity]" class="form-control quantity" onkeyup="cal()" id="qty_invoice" style="width: 70px"></td>
      <td>
        <select name="items[0][inv_item]" class="form-control item" style="width: 250px">
          <option>Items</option>
        </select>
      </td>
      <td><input type="text" name="items[0][inv_desc]" class="form-control description" style="width: 400px"></td>
      <td><span class="tag tag-success"><input type="text" name="items[0][inv_price]" class="form-control u_price" onkeyup="cal()" id="price_invoice" style="width: 150px"></span></td>
      <td><span class="tag tag-success"><input type="text" name="items[0][inv_tax]" class="form-control tax" style="width: 150px"></td>
      <td><span class="tag tag-success"><input type="text" name="items[0][inv_total]" id="total" class="form-control amount" style="width: 150px"></td>
      <td>
        <span><button class="btn btn-default" type="button" onclick="remove()"><i class="fa fa-trash" aria-hidden="true"></i></button></span>
        <span><button type="button" onclick="myFunction()" class="btn btn-default"><i class="fa fa-plus" aria-hidden="true"></i> Add Row</button></span>
      </td>

    </tr>


  </tbody>
</table>