vlookup错误(无法获取worksheetfunction类的vlookup属性)

时间:2019-01-07 10:48:21

标签: excel vba

我在vlookup上出现错误,错误提示“无法获取worksheetfunction类的vlookup属性”

这是我的代码:

Private Sub Yes_Click()

Dim input_value As Variant
Dim rg As Range
Set rg = Sheet2.Range("B8:C17")

msg = InputBox("What is your name?")
If msg = WorksheetFunction.VLookup(msg, rg, 2) Then
Yes.Value = True
Else
MsgBox ("Name already in database.")
Yes.Value = False
End If
End Sub

如果我键入的名称已经在数据库中,则没有错误。但是,名称时出现错误

1 个答案:

答案 0 :(得分:1)

请勿使用该功能。我是VBA,为此有一种“查找”方法

Dim findmsg as Range
Dim rg As Range
Set rg = Sheets("Sheet2").Range("B8:C17")
msg = InputBox(“What is your name?”)

Set findmsg = rg.find(msg)
If not findmsg is nothing then     'This condition means the names is on the database
   MsgBox(“Name already exists in database”)
   Yes.value=false
Else
   Yes.value = true
End if