Excel Userform VBA VLOOKUP

时间:2017-01-20 13:49:50

标签: vba excel-vba excel-formula userform excel

我正在创建一个userform,根据名为“ContractsList”的项目的下拉列表,我想要一个Vlookup公式来返回“TextBox 1”中的文本数据。

我收到一条错误消息“运行时错误'1004':无法获取工作表函数类的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

1 个答案:

答案 0 :(得分:0)

最后按照以下方式开始工作:

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“)

谢谢大家的帮助。

相关问题