防止将重复项从Listbox1添加到Listbox2(VBA excel) [英] Prevent duplicates from adding items from Listbox1 to Listbox2 (VBA excel)

查看:879
本文介绍了防止将重复项从Listbox1添加到Listbox2(VBA excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个ListBox。 ListBox1列出了可以通过双击该项目或按下添加按钮,用户可以选择传送到ListBox2的项目。我现在要做的是防止用户在ListBox2中添加重复项。如果检测到重复,则消息将提示已包含项目并结束代码。我猜这可以用包含吗?但是我不知道该怎么做。我有以下代码:

 '报告列表
Private Sub UserForm_Initialize()
'报表列表
With ListBox1
.AddItemReport 1
.AddItemReport 2
.AddItemReport 3
.AddItemReport 4
.AddItemReport 5
.AddItemReport 6
End with
End Sub

'将选择添加到ListBox2
Private Sub AddButton_Click()

With ListBox1
Dim itemIndex As Integer
for itemIndex = .ListCount - 1 To 0 Step -1
如果.Selected(itemIndex)然后
ListBox2 .AddItem .List(itemIndex)
结束如果
下一个itemIndex
结束

End Sub

'双击添加
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
End Sub


解决方案

如果任何人仍然感兴趣,还有另一种方法可以使用类似的技术。

  Sub Duplicate()

dim i as integer
dim x as integer
x = 0

对于i = 0 to listbox2.count - 1
如果listbox2.list(i)= myval Then
x = x + 1
End If
Next i

如果x = 0然后
listbox2.additem myval
如果

End Sub

myval是listbox1中选定的值。



单一引用您的列表中的值,它将启动一个计数器。如果没有找到您的值的实例,它会将其插入到列表框中。



希望这有助于某人。


I have two ListBoxes. ListBox1 has list of items that can be selected by the user to transfer to ListBox2 by either double clicking the item or pressing the add button. What I want to do now is to prevent the user from adding duplicates in ListBox2. If ever a duplicate is detected a message will prompt "Item already included" and end the code. I am guessing this can be done with contains? But I have no idea how to do it. I have the following codes:

'Report Listing
Private Sub UserForm_Initialize()
    'List of Reports
    With ListBox1
        .AddItem "Report 1"
        .AddItem "Report 2"
        .AddItem "Report 3"
        .AddItem "Report 4"
        .AddItem "Report 5"
        .AddItem "Report 6"
    End With
End Sub

'Add selection to ListBox2
Private Sub AddButton_Click()

        With ListBox1
        Dim itemIndex As Integer
        For itemIndex = .ListCount - 1 To 0 Step -1
            If .Selected(itemIndex) Then
                ListBox2.AddItem .List(itemIndex)
            End If
        Next itemIndex
    End With

End Sub

    'Double click to Add
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
End Sub

解决方案

In case anyone is still interested, there's another way to do this, using a similar technique.

Sub Duplicate()

 dim i as integer
 dim x as integer
 x = 0

  For i = 0 to listbox2.count - 1 
     If listbox2.list(i) = myval Then
        x = x + 1
     End If
  Next i

  If x = 0 Then
     listbox2.additem myval
  End If

End Sub

Where myval is the selected value from listbox1.

Essentially if it finds a single reference to your value in the list, it will start a counter. If no instances of your value are found, it will insert it into the listbox.

Hope this helps someone.

这篇关于防止将重复项从Listbox1添加到Listbox2(VBA excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆