Frage

I am attempting to have VBA check whenever a new employee ID is entered into the sheet (automatically looks up their information from a database) that if the ID that is entered returns an individual that is considered an associate, a message box will generate informing the user that they are about to select an associate. Currently I am getting a run-time error 424 at the If Application line. I am very new to VBA so if I am making a silly mistake I'm sorry. Any help is greatly appreciated.

Edited Code - Thanks to Doug and Simoco for the help.

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
War es hilfreich?

Lösung

Try this code:

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top