Vlookup基于单元格输入,根据vlookup结果警告用户

时间:2014-02-16 16:18:52

标签: excel vba vlookup

每当在工作表中输入新的员工ID(自动从数据库中查找他们的信息)时,我试图检查VBA,如果输入的ID返回被认为是关联的个人,则会显示一个消息框生成通知用户他们将要选择一个关联。目前我在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

1 个答案:

答案 0 :(得分:2)

试试这段代码:

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