使用不同的行填充2个列表框

时间:2018-03-14 19:03:12

标签: excel vba excel-vba filter listbox

我的用户表单上有1个组合框和2个列表框:

  1. cbox_rrhh
  2. lbox_por
  3. lbox_done
  4. enter image description here

    和一张纸" Pedidos"像这样:

    ![enter image description here

    我找到了一种方法,可以在组合框值之后过滤列表框数据,这是{" J"列ex(roraima,karla,yvonne,sara)但我还需要在lbox_por上显示行,如果列" J"比赛" Si"如果是"否"必须在lbox_done"。

    中加载这些行

    使用的代码:

        Private Sub cbo_rrhh_Change()
    Dim Cell As Range                   'defining your searcharea
    Dim Row_Counter As Integer          'rowno
    Dim Pos As Integer                  'rowno in array
    Dim MyList() As String              'the array
    Dim No_Pos As Integer               'total rows in aray
    Dim Search_name As String           'searchvalue in combobox
    Dim Real_last_row As Integer        'last row in "Pedidos"
    
    'xllastrow is a function
    
    Real_last_row = xlLastRow("Pedidos") 'last row in "Pedidos"
    
    Search_name = cbo_rrhh.Value
    
    'Searching in "Pedidos" for matches with searchstring
    
    For Each Cell In Worksheets("Pedidos").Range("J2:J" & Real_last_row)
        If Cell Like "*" & Search_name & "*" Then
    'If a value matches with the searchstring, number of rows of array is + 1
            No_Pos = No_Pos + 1
        End If
    Next Cell
    
    Row_Counter = 2                  'start of the nameslist
    Pos = 0                          'rowno in array, beginning with zero
    ReDim Preserve MyList(No_Pos, 8) 'Redimming of array with total no of rows and 3 columns
    
    'if value isn't in searchstring then rowno is rowno + 1
    'if a match is found the array is filled and the rowno in array is + 1
    
    For Each Cell In Worksheets("Pedidos").Range("J2:J" & Real_last_row)
        If Cell Like "*" & Search_name & "*" Then
            MyList(Pos, 0) = Worksheets("Pedidos").Range("A" & Row_Counter)
            MyList(Pos, 1) = Worksheets("Pedidos").Range("B" & Row_Counter)
            MyList(Pos, 2) = Worksheets("Pedidos").Range("C" & Row_Counter)
            MyList(Pos, 3) = Worksheets("Pedidos").Range("E" & Row_Counter)
            MyList(Pos, 4) = Worksheets("Pedidos").Range("F" & Row_Counter)
            MyList(Pos, 5) = Worksheets("Pedidos").Range("G" & Row_Counter)
            MyList(Pos, 6) = Worksheets("Pedidos").Range("H" & Row_Counter)
            MyList(Pos, 7) = Worksheets("Pedidos").Range("I" & Row_Counter)
            MyList(Pos, 8) = Worksheets("Pedidos").Range("J" & Row_Counter)
            Pos = Pos + 1
            Row_Counter = Row_Counter + 1
        Else
            Row_Counter = Row_Counter + 1
        End If
    Next Cell
    Application.ShowToolTips = True
    
    With lbox_por
        .ColumnCount = 10                           'no of columns (0,1,2)
        .ControlTipText = "Pedidos pendientes ..."    'tiptext
    
    End With
    With lbox_done
        .ColumnCount = 9                           'no of columns (0,1,2)
        .ControlTipText = "Pedidos realizados ..."    'tiptext
    
    End With
    lbox_por.List = MyList                          'define the list of listbox1
    lbox_done.List = MyList
    
    End Sub
    

    和上下文: Fecha:指日期。 Palabras:单词。 塔萨:率。 蒙托:金额。 帕加多:付出了代价。 Redactor:作家。

1 个答案:

答案 0 :(得分:0)

对于一个列表框(lbox_done),您无法更改:

If Cell Like "*" & Search_name & "*" Then

对此:

If Cell Like "*" & Search_name & "*" And Worksheets("Pedidos").Range("I" & Row_Counter) = "NO" Then

然后您可以创建一个重复列表(即MyList2),条件已更改为:

If Cell Like "*" & Search_name & "*" And Worksheets("Pedidos").Range("I" & Row_Counter) = "SI" Then

您可以将其指定给其他列表框 - lbox_por