Vba Listbox1包含listbox2中的文本->;删除项目
我有一个带有两个列表框的VBA用户表单。我正在努力进行以下操作: 我需要检查Listbox1是否包含Listbox2中的列表项,如果包含,则删除Listbox1中包含Listbox2中的项目。 例如,listbox1中的项目“紫色猴子”包含“猴子”(listbox2中的项目),因此我需要删除整个项目“紫色猴子” 有人能帮我吗 我使用以下代码创建主列表(关键字)并初始化userform。此外,我还创建了一个文本框,用户在其中输入项目并将其添加到Listbox2。此代码运行良好:Vba Listbox1包含listbox2中的文本->;删除项目,vba,excel,listbox,userform,Vba,Excel,Listbox,Userform,我有一个带有两个列表框的VBA用户表单。我正在努力进行以下操作: 我需要检查Listbox1是否包含Listbox2中的列表项,如果包含,则删除Listbox1中包含Listbox2中的项目。 例如,listbox1中的项目“紫色猴子”包含“猴子”(listbox2中的项目),因此我需要删除整个项目“紫色猴子” 有人能帮我吗 我使用以下代码创建主列表(关键字)并初始化userform。此外,我还创建了一个文本框,用户在其中输入项目并将其添加到Listbox2。此代码运行良好: Private S
Private Sub UserForm_Initialize()
Application.Visible = False
Keywords.SetFocus
TextBox2.Value = NegKeyList.ListCount & "negative keys"
Dim mycollection As Collection, cell As Range
On Error Resume Next
Set mycollection = New Collection
With Keywords
.Clear
For Each cell In Worksheets("Rawdata").Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If Len(cell) <> 0 Then
Err.Clear
mycollection.Add cell.Value, cell.Value
If Err.Number = 0 Then .AddItem cell.Value
End If
Next cell
End With
MsgBox mycollection.Count & "added to the list"
If Keywords.ListCount > 0 Then
TextBox1.Value = Keywords.ListCount & " keys"
End If
End Sub
Private子用户表单_Initialize()
Application.Visible=False
关键词.SetFocus
TextBox2.Value=NegKeyList.ListCount&“负键”
Dim mycollection作为集合,cell作为范围
出错时继续下一步
设置mycollection=新集合
带关键字
清楚的
对于工作表中的每个单元格(“Rawdata”).Range(“A3:A”和单元格(Rows.Count,1).End(xlUp).Row)
如果Len(cell)为0,则
呃,明白了
mycollection.Add cell.Value,cell.Value
如果Err.Number=0,则.AddItem cell.Value
如果结束
下一个细胞
以
MsgBox mycollection.Count&“已添加到列表”
如果Keywords.ListCount>0,则
TextBox1.Value=Keywords.ListCount&“keys”
如果结束
端接头
我现在需要做另一个功能,用户可以按下按钮并删除包含ListBox2项的所有关键字(不一定等于它们)。假设您的用户表单上有一个命令按钮(CommandButton1),您可以将代码放入
CommandButton1\u Click()
事件中。由于某些项目可能会从ListBox1中删除,counter1
需要从最大到最小,以避免计数器的数组索引问题变得大于最大索引
Dim counter1 as Long
Dim counter2 as Long
For counter1 = ListBox1.ListCount - 1 to 0 Step -1 'Indexes are 0-based, so max index is count - 1
For counter2 = 0 to ListBox2.ListCount - 1
If InStr(1, ListBox1.List(counter1), ListBox2.List(counter2)) > 0 Then 'InStr returns 0 when there's no match
ListBox1.RemoveItem counter1
Exit For 'Skip any more compares for the deleted Item
End If
Next counter2
Next counter1
假设您的用户表单上有一个命令按钮(CommandButton1),您可以将代码放入
CommandButton1\u Click()
事件中。由于某些项目可能会从ListBox1中删除,counter1
需要从最大到最小,以避免计数器的数组索引问题变得大于最大索引
Dim counter1 as Long
Dim counter2 as Long
For counter1 = ListBox1.ListCount - 1 to 0 Step -1 'Indexes are 0-based, so max index is count - 1
For counter2 = 0 to ListBox2.ListCount - 1
If InStr(1, ListBox1.List(counter1), ListBox2.List(counter2)) > 0 Then 'InStr returns 0 when there's no match
ListBox1.RemoveItem counter1
Exit For 'Skip any more compares for the deleted Item
End If
Next counter2
Next counter1
如果您已经有了一些代码,您是否可以包括您尝试过的内容以及遇到的问题?如果没有,我建议查看
InStr()
和列表框的。添加和。删除方法……您需要遍历ListBox1
,然后为ListBox2
嵌套另一个循环,并查看索引值是否相等(如果ListBox1(I)=ListBox2(j),然后是ListBox.removietem I
)谢谢我可以执行循环来比较列表并删除重复项,但我需要确保删除Listbox1中包含与Listbox2不相同的项目(文本)。Mistella,我已经添加了代码,但这就是我所拥有的。我无法理解如何使用.Add和.Remove for.contain功能,我在问题末尾提到了这些功能。你能给我一个想法吗?如果你已经有了一些代码,你能包括你尝试过的,以及你遇到的困难吗?如果没有,我建议查看InStr()
和列表框的。添加和。删除方法……您需要遍历ListBox1
,然后为ListBox2
嵌套另一个循环,并查看索引值是否相等(如果ListBox1(I)=ListBox2(j),然后是ListBox.removietem I
)谢谢我可以执行循环来比较列表并删除重复项,但我需要确保删除Listbox1中包含与Listbox2不相同的项目(文本)。Mistella,我已经添加了代码,但这就是我所拥有的。我无法理解如何使用.Add和.Remove for.contain功能,我在问题末尾提到了这些功能。你能给我一个主意吗?先生,我正在尝试使用代码,但它在Listbox1(counter1)上给了我类型不匹配的错误。@Garuzz抱歉,请尝试更新的版本。我将其更改为ListBox1.List(counter1)
,并将其更新为ListBox2
。是的!非常感谢!成功了!!!只需将Listbox2.Count更正为Listbox2.Listcount。@Garuzz请导航到此解决方案左侧的复选框,如果这对您有效,请接受该解决方案!清晰高效+1)。放大说明:与AddItem
方法不同(更喜欢使用更快的数组!),removietem
方法也适用于超过10个列表列。Mistella,我正在尝试使用代码,但它在Listbox1(counter1)上给了我类型不匹配错误。@Garuzz抱歉,请尝试更新版本。我将其更改为ListBox1.List(counter1)
,并将其更新为ListBox2
。是的!非常感谢!成功了!!!只需将Listbox2.Count更正为Listbox2.Listcount。@Garuzz请导航到此解决方案左侧的复选框,如果这对您有效,请接受该解决方案!清晰高效+1)。放大说明:与AddItem
方法不同(最好使用更快的数组!),removietem
方法也适用于超过10个列表列。