VBA填充列表框将全部出现

时间:2015-10-27 21:20:41

标签: excel-vba vba excel

我有一个包含4列和35000行的电子表格。

我制作了一个包含3个列表框(以及其他按钮)的表单

当我点击搜索按钮时,我想在列A中搜索所有出现的数字,并用相应的B,C和C填充列表框。 d

(如果它有助于速度,那么相同数字的出现次数绝不会超过10次,并且A列被排序)

123狗Fido Elm $ 50

123 dog Spot Oak $ 40

456 Cat Jet Adam $ 30

搜索123和

listbox 1将显示Fido&点 列表框2将显示Elm&橡木 listbox3 ....

到目前为止

代码:

Private Sub cmdSearch_Click()

Dim Response As Long
Dim NotFound As Integer


NotFound = 0


ActiveWorkbook.Sheets("test").Activate

Frame1.Visible = False

Response = txtItemNumber.Text

If Response <> False Then

Range("A2").Select


Do Until ActiveCell.Value = Val(Response)
If ActiveCell.Value = "" Then
MsgBox "Item Number Not Found!", vbExclamation
NotFound = 1
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop

If ActiveCell.Value = Val(Response) Then
Frame1.Visible = True

ListBox1.Text = ActiveCell.Offset(0, 1)
ListBox2.Text = ActiveCell.Offset(0, 2)
ListBox3.Text = ActiveCell.Offset(0, 3)

**'would like to see if next number in Column A is also the same as being searched... if so then add to listbox'
'do this until the next number doesn't match'**


End If

End If

End Sub

也可以使用数组或vlookup来加速进程?我不熟悉那些 我想要多个列表框而不是多列列表框(如果出现的话)

2 个答案:

答案 0 :(得分:0)

好奇,如果你将Response定义为long,为什么你在while循环中使用val(Response)?如果它很长,那么Response = txtItemNumber.Text可能会因为尝试将字母数字字符串仅存储在数字中而导致错误。

由于您将响应变为长,请改为使用此...

Response = Val("0" & txtItemNumber.Text)

这假定没有负数。通过预先归零,你不会改变数字的值(0100仍然是100),但你也可以处理&#34; 0abc&#34;如果用户输入文本则会出现问题如果用户输入&#34; abc&#34;它将返回0。并且您的解决方案已经检查0(错误)。如果用户输入&#34; 100&#34;它转换了&#34; 0100&#34;到100,然后你的解决方案继续使用该值。如果用户输入&#34; -100&#34;它将失败自&#34; 0-100&#34;不是100。

现在,由于我们使用的是long,因此删除其他地方对Val(Response)的所有引用。这将减少搜索单元格值的循环内的类型转换。正如你所拥有的那样,每次循环它都会将Response从long转换为字符串然后转换为long,然后将UNTIL进行比较。它完成了35000次。

通过在循环之前使用Val进行转换,它现在将字符串转换为长转换ONCE,而不是循环通过单元格的所有35000次。它是一个微小的变化,可以加快一点。

答案 1 :(得分:0)

此代码基于您的想法,但它使用名为arr的数组来快速输入数据并促进快速处理。定位的引用存储在字符串中,稍后将其拆分为ListBox。

垂直条用作拆分字符。如果这与您的数据发生冲突,则需要对其进行更改。

注意:我不认为你的Frame1.Visible命令是有效的。实际上做了什么。

Private Sub cmdSearch_Click()

    Dim Response As Long
    Dim NotFound As Integer
    Dim arr As Variant
    Dim i As Long
    Dim str1 As String, str2 As String, str3 As String

    NotFound = 0
    ActiveWorkbook.Sheets("test").Activate
    Frame1.Visible = False
    Response = txtItemNumber.Text

    If Response <> False Then

        With ActiveSheet
            arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        End With

        For i = 1 To UBound(arr)
            If arr(i, 1) = Response Then
                str1 = IIf(str1 = "", arr(i, 2), str1 & "|" & arr(i, 2))
                str2 = IIf(str2 = "", arr(i, 3), str2 & "|" & arr(i, 3))
                str3 = IIf(str3 = "", arr(i, 4), str3 & "|" & arr(i, 4))
            End If
        Next

        If str1 = "" Then
            MsgBox "Item Number Not Found!", vbExclamation
            NotFound = 1
        Else
            Frame1.Visible = True
            ListBox1.List = Split(str1, "|")
            ListBox2.List = Split(str2, "|")
            ListBox3.List = Split(str3, "|")
        End If

    End If

End Sub