基于单元格input的Vlookup根据vlookup结果警告用户

我试图有一个新的员工IDinput到工作表(自动从数据库中查找他们的信息)的VBA检查,如果input的ID返回一个被认为是关联的个人,消息框将生成通知用户他们即将select一个关联。 目前,我在If Application行遇到了运行时错误424。 我对VBA很新,所以如果我犯了一个愚蠢的错误,我很抱歉。 任何帮助是极大的赞赏。

编辑的代码 – 感谢Doug和Simoco的帮助。

Sub Worksheet_Change(ByVal Target As Range) Dim WatchRange As Range Dim IntersectRange As Range Set WatchRange = Range("A2:A550") Set IntersectRange = Intersect(Target, WatchRange) If IntersectRange Is Nothing Then 'Do Nothing Spectacular Else On Error Resume Next If WorksheetFunction.VLookup(IntersectRange, Me.Range("A2:M550"), 14, False) = "Associate" Then MsgBox "You have choosen an associate for this trip!" Else End If End If End Sub 

试试这个代码:

 Sub Worksheet_Change(ByVal Target As Range) Dim res As Variant If Not Intersect(Target, Range("A2:A550")) Is Nothing Then res = Application.CountIfs(Range("A2:A550"), Target, _ Range("M2:M550"), "Associate") If Not IsError(res) Then If res > 0 Then MsgBox "You have choosen an associate for this trip!" End If End If End Sub