我有这个代码,当我点击我的列表框中的项目时,我正在使用它来搜索范围。我从来没有通过列表框循环,并想知道如何添加一个循环来执行我所需要的,而无需单击列表框中的每个项目。这里是我使用的代码:Excel VBA通过列表框循环
Sub FindListValue()
Dim FirstAddress As String
Dim rSearch As Range 'range to search
Dim c As Range
With Sheets("PN-BINS")
Set rSearch = .Range("B1", .Range("B65536").End(xlUp))
End With
Dim i As Long
' loop through all items in ListBox1
For i = 0 To Me.ListBox1.ListCount - 1
' current string to search for
strFind = Me.ListBox1.List(i)
With rSearch
Set c = .Find(strFind, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then 'found it
c.Select
Me.ListBox1.AddItem strFind & " | " & c.Offset(0, -1).Value, Me.ListBox1.ListIndex + 1
Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
'Exit Sub
Else: 'MsgBox strFind & " is not listed!" 'search failed
End If
End With
' the rest of your code logics goes here...
Next i
End Sub
我不确定我是否理解这个问题。你已经从你的列表框中选择了一些东西,并且你想使用这个值来找到某个范围内的东西(你可以通过循环或Find方法来完成) - 那么点击列表框中的每个项目进入它? – jsheeran
当我单击列表框中的项目时,如果单击每个项目,我将使用范围内的搜索信息替换该行,该范围工作得很好。我想要做的是不需要单击列表框中的所有项目,只需搜索列表框中的每一行即可。如果发现,请更换线路。如果没有找到,就什么也不做。我希望我有道理。 – Noob2Java
@ user3340949尝试下面我的答案中的代码,看看它是否适用于您 –