将3个列表框转换为1个3列列表框?

时间:2015-10-30 21:13:00

标签: excel-vba vba excel

以下代码在项目#中搜索A列(已排序),每次找到它时,相应的B,C& D列输入3个列表框。我想使用3列列表框。有什么帮助吗?

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("Items").Activate

    Response = Val("0" & Replace(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

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

这应该这样做:

变化:

    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

为:

If str1 = "" Then
  MsgBox "Item Number Not Found!", vbExclamation
  NotFound = 1
Else
  Frame1.Visible = True
  ListBox1.Clear 'to avoid errors
  ListBox1.ColumnCount = 3

  For i = 0 To UBound(Split(str1, "|"))
    ListBox1.AddItem Split(str1, "|")(i)
    ListBox1.List(i, 1) = Split(str2, "|")(i)
    ListBox1.List(i, 2) = Split(str3, "|")(i)
  Next
End If

提示:您可以更改ColumnWidths

然而......为了给你一些工作,我建议将它与你的arr合并 - 部分...... 只是使用我的解决方案将是一种浪费:D