首页 > 解决方案 > 使用用户表单进行 Vlookup

问题描述

我正在尝试使用 3 个文本框配置 Userform 研究,但我无法使其工作,也不知道为什么。

这是我的代码:

Private Sub TextBox1_AfterUpdate()
On Error GoTo 1
If WorksheetFunction.CountIf(Sheets("Feuil1").Range("A:A"), Me.TextBox1.Value) = 0 Then

 MsgBox "introuvable"
End If
With Me
 .TextBox2 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox1), Feuil1.Range("A:E"), 2, 0)
End With
1
End Sub

希望得到你的帮助

谢谢

标签: excelvbavlookupuserform

解决方案


删除WorksheetFunction然后如果没有匹配则不会出现运行时错误:

Private Sub TextBox1_AfterUpdate()
    Dim r

    r = Application.VLookup(CLng(Me.TextBox1), Feuil1.Range("A:E"), 2, False)

    Me.TextBox2 = IIf(IsError(r),"Introuvable", r)

End Sub

推荐阅读