Excel 用户表单 VBA VLOOKUP

Excel Userform VBA VLOOKUP

我正在创建一个用户表单,其中基于名为 "ContractsList" 的项目下拉列表我想要一个 Vlookup 公式 return "TextBox 1" 中的文本数据。

我收到一条错误消息说“运行-time error'1004': 无法获取工作表函数 class

的 Vlookup 属性

不确定我做错了什么,这是我的代码,如果有人能发现错误的话。

Private Sub ContractsList_AfterUpdate()

If WorksheetFunction.CountIf(Sheet2.Range("A:A"),Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub

End If
'Lookup values based on first control
With Me

.TextBox1 = Application.WorksheetFunction.VLookup(Me.TextBox1, ("B5:B72"), 2, 0)

End With
End Sub

终于让它工作如下:

Private Sub ContractsList_AfterUpdate()

If WorksheetFunction.CountIf(Sheet2.Range("A:A"), Me.ContractsList.Value) = 0 Then
MsgBox "This contract is not on the list"
Me.ContractsList.Value = ""
Exit Sub

End If
'Lookup values based on first control
With Me

.TextBox1 = Application.WorksheetFunction.VLookup(Me.ContractsList, Sheet2.Range("A5:E72"), 2, 0)

End With
End Sub

我只需要添加 "Sheet2.Range("A5:E75")

谢谢大家的帮助。