2015-10-30 40 views
0

以下代碼在列A(已排序)中搜索項#,並且每次找到它時,都會將相應的B,C列輸入到3個列表框中。我想使用3列的列表框。任何幫助?將3個列表框變成1個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 

感謝您的幫助......

+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

+0

我選擇暫時離開程序......自發布問題以來,我已經寫了更多內容,但會在完成原創後嘗試回來,以便在您的幫助和建議下進行改進 – instanceoftime