第一张图片显示时有2列范围,第2列列表框,文本框和命令按钮的用户表单。
是否有代码,这样如果我在文本框中输入一个值,那么代码会搜索我的范围,直到找到该值并从另一列返回相应的值,并且所有以下值都将达到第一个值第一列的空白细胞。
例如,如果我输入" DDD"在文本框中,列表框中的第一列将显示" DDD"第二个将分别显示444,555和666。
这是我正在使用的代码,但是当我输入" AAA"在文本框中,.end(xldown)转到" DDD"而不是" BBB"。有没有办法解决这个问题?
提前感谢你。
Dim SearchTerm As String
Dim topCell As Range, BottomCell As Range
SearchTerm = TextBox1.Text
With Sheet1.Range("A:A")
Set topCell = .Find(SearchTerm, after:=.Cells(Rows.Count, 1), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)
If topCell Is Nothing Then
MsgBox SearchTerm & " not found."
Else
Set BottomCell = Range(topCell.End(xlDown).Offset(-1, 0), .Cells(Rows.Count, 2).End(xlUp)).Cells(1, 2)
With ListBox1
.Clear
.List = Range(topCell, BottomCell).Value
End With
End If
End With
答案 0 :(得分:0)
使用下面的代码作为参考,以确定哪个单元格实际上是值范围的底部单元格。如果您需要帮助,请告诉我。
Sub DoTheThang()
Dim TopCell As Range
Dim BottomCell As Range
Dim SearchString As String
Dim rngUsed As Range
SearchString = "EEE"
Set TopCell = Range("A:A").Find(SearchString, Cells(1, 1))
Set BottomCell = TopCell
Set rngUsed = Sheet1.UsedRange
Do While BottomCell.Offset(1).Value = "" And Not Intersect(BottomCell, rngUsed) Is Nothing
Set BottomCell = BottomCell.Offset(1)
Loop
MsgBox TopCell.Address
MsgBox BottomCell.Address
End Sub