【发布时间】:2017-06-20 09:12:14
【问题描述】:
我之前在这个论坛上看到过类似的案例,但我还没有看到针对我的确切问题的适当解决方案,所以就这样吧......
基本上,我有一个带有列表框的用户表单,用于管理数据表中的数据。有多个整体与每个“条目 ID”相关联(在 A 列中)。当我在搜索框中搜索“条目 ID”时,我想在列表框中查看所有关联条目,每个条目都有列 A 到 J(因此代码需要循环并查找多行,然后“复制”将每一行的多列放入列表框中,我认为这是我正在努力的地方)。
我采用了我在论坛上为类似案例找到的一段代码,但似乎无法复制多个列:
Private Sub cmdFind_Click()
Dim sht As Worksheet
Dim lastrow As Variant
Dim strSearch As String
Dim aCell As Range
Dim row_number As Integer
Dim item_in_review As Variant
Set sht = ActiveWorkbook.Sheets("a")
lastrow = sht.Range("A" & Rows.Count).End(xlUp).Row
strSearch = txtSearch.Text
Set aCell = sht.Range("A1:A" & lastrow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
GoTo wfrefvalid
Else
MsgBox "Oops! That Work File does not exist. Please try again.", Title:="Try again"
txtSearch.Value = ""
End If
Exit Sub
wfrefvalid:
row_number = 3
'clears the listbox so that you have dont have a continuously growing list
lstSearch.Clear
Do
DoEvents
row_number = row_number + 1
item_in_review = sht.Range("A" & row_number)
If item_in_review = txtSearch.Text Then
lstSearch.AddItem sht.Range("A" & row_number & ":J" & row_number)
End If
Loop Until item_in_review = ""
End Sub
我很确定这不是做我想做的事情的正确方法,但我找不到任何完全相似的案例可以作为基础。
非常感谢任何提示。
编辑:@braX 帮助我删除了运行时错误(代码已更新),但列表框中没有上传任何数据...也许我没有正确引用“行”循环或 addItem 函数但不确定他们中的哪一个?
【问题讨论】: