使用手动输入自动完成Excel VBA问题



大家都有一些Excel VBA代码的问题。我使用多个组合框,但遇到问题,公司表只是一堆可能的公司可以选择,也有代码允许自动完成。

如果盒子是空的,按tab键填写第一个盒子与第一项列表(好)

如果用户键入不在列表上的东西,这就是它遇到问题的地方,即。value = . list(0),并在调试时崩溃VBA,(无法获得列表属性无效的属性数组索引)。以来的一个组合框列表需要支持的能力,选择从列表或手动输入,会推荐做Vlookup/VBA内匹配,或者做类似的价值=公司,那么进入待他们。

在这个问题上的任何帮助都将不胜感激。

Private Sub Company_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Debug.Print Time; "KeyDown"; KeyCode; Company.ListIndex; Company.ListCount, Company.Value
IsArrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
If KeyCode = vbKeyReturn Then
Me.Company.List = Worksheets("Company_Table").Range("A2", Worksheets("Company_Table").Cells(Rows.Count, "A").End(xlUp)).Value
ElseIf KeyCode = vbKeyTab Then
'Tab key selects first displayed item or highlighted item
With Me.Company
If .ListIndex = -1 Then
.Value = .List(0)
Else
.Value = .List(.ListIndex)
End If
End With
KeyCode = vbKeyReturn
End If
End sub

Try This

Private Sub Company_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
On Error GoTo ERRHAND
Debug.Print Time; "KeyDown"; KeyCode; Company.ListIndex; Company.ListCount, Company.Value
IsArrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
If KeyCode = vbKeyReturn Then
Me.Company.List = Worksheets("Company_Table").Range("A2", Worksheets("Company_Table").Cells(Rows.Count, "A").End(xlUp)).Value
ElseIf KeyCode = vbKeyTab Then
'Tab key selects first displayed item or highlighted item
With Me.Company
If .ListIndex = -1 Then
.Value = .List(0)
Else
.Value = .List(.ListIndex)
End If
End With
KeyCode = vbKeyReturn
End If
Exit Sub
ERRHAND:
Err.Clear
Me.Company.Value = "Value Not Found"
End Sub

最新更新