我有下面的用户表单,并希望只显示所选员工的列表框结果。当我在组合框中选择第一个名字时,我会得到正确的结果。
但是当我选择任何其他组合框选项时,所有数据都进入列表框的第一列。
我做错了什么?
下面是我用来填充列表框的当前VBA代码:
Private Sub cbxEAName_Change()
Set shData = ThisWorkbook.Sheets("Data")
Dim rng As Range
Set rng = shData.Range("C2:G" & shData.Range("A" & shData.Rows.Count).End(xlUp).Row)
Dim filteredData() As Variant
Dim i As Long
Dim j As Long
Dim numRows As Long
numRows = 0
For i = 1 To rng.Rows.Count
If rng.Cells(i, 1).Value = EmployeeAnalysis.cbxEAName.Value Then
numRows = numRows + 1
ReDim Preserve filteredData(1 To rng.Columns.Count, 1 To numRows)
For j = 1 To rng.Columns.Count
filteredData(j, numRows) = rng.Cells(i, j).Value
Next j
End If
Next i
With EmployeeAnalysis.lbxEmployeeResults
.Clear
.ColumnCount = rng.Columns.Count
If numRows > 0 Then
.List = Application.Transpose(filteredData)
End If
.ColumnWidths = "90;100;100;100;50"
.TopIndex = 0
End With
结束接头
1条答案
按热度按时间ymdaylpp1#
尝试
这将找到列C中的组合框值,然后将列C-D-E-F-G中的相应值添加到列表框中