Excel vlookup错误(无法获取工作表函数类的vlookup属性);

Excel vlookup错误(无法获取工作表函数类的vlookup属性);,excel,vba,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) The

我在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

如果我键入的名称已在数据库中,则不会出错。但是,如果名称不使用该函数,则会出错。我是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

谢谢,但是有一个错误。VBA突出显示了单词find(第三行)并说“method or data member not found”(方法或数据成员未找到)。我没有在我的代码中添加您设置为范围的内容。请尝试添加它。此外,我不知道您是否将sheet2声明为变量,如果您没有将正确的范围设置为rg=Sheets(sheet2)。范围(“B8:C17”)很抱歉,如何修改代码以使其找到精确的匹配项?因为例如,数据库中的名称是Mary。当我键入Mar时,系统表示该名称已存在于数据库中。请尝试添加此findmsg=rg.find(msg,Lookat:=xlWhole)