Excel 完成组合框VBA时出现不匹配错误
当试图在userform中键入组合框以查找匹配项时,我遇到了一个问题。当我输入错误的字母/数字时,它会立即给出一个不匹配错误,并将我指向VBA代码。我怎样才能避免呢?有什么我可以添加到我的代码或更改属性吗?因为对于用户来说,输入错误是很常见的,我不想将用户重定向到代码 这是我的组合框的代码:Excel 完成组合框VBA时出现不匹配错误,excel,vba,combobox,userform,Excel,Vba,Combobox,Userform,当试图在userform中键入组合框以查找匹配项时,我遇到了一个问题。当我输入错误的字母/数字时,它会立即给出一个不匹配错误,并将我指向VBA代码。我怎样才能避免呢?有什么我可以添加到我的代码或更改属性吗?因为对于用户来说,输入错误是很常见的,我不想将用户重定向到代码 这是我的组合框的代码: Private Sub ComboBox3_Change() If Me.ComboBox3.Value <> "" Then Dim sh As Worksheet Set
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
End Sub
Private Sub UserForm_Activate()
Dim i As Integer
Me.ComboBox3.Clear
Me.ComboBox3.AddItem ""
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem sh.Range("A" & i).Value
Next i
您需要使用错误处理语句跳过生成错误的部分
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
or error go to MyHandler
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
MyHandler:
' Expected behavior on error
End Sub
我应该在我的处理器上写什么?我以前没有使用过它,如果您只想忽略错误,可以保持原样。如果您想在遇到错误时执行某些操作,则必须将代码放在那里,如“msgbox错误输入”等。。