比较两个列表框的匹配值

时间:2021-02-20 18:25:09

标签: excel vba

我正在尝试实现一种为特定工作分配员工的简单方法。

有表 1(员工技能)和表 2(工作要求)。我希望能够检查员工是否具备足够的技能来胜任某项工作。

这样,我有两个列表框,Listbox1 作为 Table1,Listbox2 作为 Table2。我有以下代码来查找 Listbox1 和 Listbox2 中特定行的索引

Dim i As Integer
Dim j As Integer
With ListBox1
.MultiSelect = fmMultiSelectSingle
.ListIndex = -1
.MultiSelect = fmMultiSelectMulti
For i = 0 To .ListCount - 1
 For j = 0 To .columnCount - 1
 If ComboBox2.Value = .Column(j, i) Then
 .ListIndex = i
 .Selected(i) = True
 End If
 Next j
Next i
End With

在 Listbox1 和 2 中确定员工所在行和工作行后,我想检查它们之间的匹配值,并显示在 Listbox3 上匹配的值。我目前有以下代码,但仅适用于具有一列的 Listbox:

Dim i As Long, j As Long
Dim fMatch As Long

For i = 1 To ListBox1.ListCount
  fMatch = False
  For j = 1 To ListBox2.ListCount
    If ListBox1.List(i - 1) = ListBox2.List(j - 1) Then
      fMatch = True
      Exit For
    End If
  Next j
  If fMatch Then
    ListBox3.AddItem ListBox1.List(i - 1)
  End If
Next i

我也在尝试获取最适合 listbox3 角色的最佳员工名单。

enter image description here

1 个答案:

答案 0 :(得分:1)

'Identify and select employee and job rows in ListBox1 and ListBox2
...
'Display skill matches from Course1..3 columns in ListBox3
ShowMatches ListBox1.ListIndex, ListBox2.ListIndex
...    
Private Sub ShowMatches(EmployeeRow As Long, JobRow As Long)
      Dim i As Long, j As Long, course
      If EmployeeRow = -1 Then Exit Sub
      If JobRow = -1 Then Exit Sub
      ListBox3.Clear
      'skip column 0 as it doesn't contain skill information
      For i = 1 To UBound(ListBox1.List, 2)
        For j = 1 To UBound(ListBox2.List, 2)
          course = ListBox1.List(EmployeeRow, i)
          If course = ListBox2.List(JobRow, j) Then
            ListBox3.AddItem course
          End If
        Next j
      Next i
    End Sub