我有以下代码来过滤键入时的组合框列表。
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim arrIn As Variant, arrOut As Variant
Dim i As Long, j As Long
arrIn = Sheet1.Range("A2:G7")
ReDim arrOut(1 To UBound(arrIn), 1 To 2)
For i = 1 To UBound(arrIn)
If arrIn(i, 1) Like ComboBox1.Text & "*" Then
j = j + 1
arrOut(j, 1) = arrIn(i, 1)
arrOut(j, 2) = arrIn(i, 2)
End If
Next
ComboBox1.List = arrOut
End Sub
但是,我需要将数组的范围更改为 Range(“ A2:A2000”),因此,我还必须在此处进行一些更改 ReDim arrOut(1 To UBound (arrIn),1到2)。
我无法理解,因为我在数组概念和修改方面很烂。谁能帮助我?
预先感谢您:)
答案 0 :(得分:2)
如果我正确理解了您的问题,则以下代码会将Range("A2:A2000")
中的值放入数组中,然后相应地过滤ComboBox:
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim arrIn As Variant, arrOut As Variant
Dim i As Long, j As Long
arrIn = Sheet1.Range("A2:A2000")
ReDim arrOut(1 To UBound(arrIn), 1 To 1)
For i = 1 To UBound(arrIn)
If arrIn(i, 1) Like ComboBox1.Text & "*" Then
j = j + 1
arrOut(j, 1) = arrIn(i, 1)
End If
Next
ComboBox1.List = arrOut
End Sub