Home > OS >  Pop up messages in excel when entering specific words into a cell
Pop up messages in excel when entering specific words into a cell

Time:10-07

Working off a macro I found on here, I managed to get a delivery requirement pop-up to work when entering a specific postcode. However, when I try and duplicate this for multiple postcodes with different delivery requirements I get 'Compile error: Block If without End If'.

First macro is the one that works for a single postcode & the second is the one where the error message appears (both have postcodes removed for GDPR).

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:Z99")) Is Nothing Then
        If Target = "xxx xxx" Then
            MsgBox "HI-AB DELIVERY REQUIRED."
        End If
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:Z99")) Is Nothing Then
        If Target = "xxx xxx" Then
            MsgBox "HI-AB DELIVERY REQUIRED."
    If Not Intersect(Target, Range("A1:Z99")) Is Nothing Then
        If Target = "axx xx" Then
            MsgBox "NO DELIVERIES BEFORE 8AM."
        End If
    End If
End Sub

I only want the pop up to appear when the specific post code is entered. Is this possible?

Any help would be greatly appreciated!!

CodePudding user response:

You need to get familiar with the If-Then-Else Statement.

Your code can be written as

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:Z99")) Is Nothing Then
        If Target = "xxx xxx" Then MsgBox "HI-AB DELIVERY REQUIRED."
        If Target = "axx xx" Then MsgBox "NO DELIVERIES BEFORE 8AM."
    End If
End Sub

or better

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:Z99")) Is Nothing Then
        Select Case Target
            Case "xxx xxx": MsgBox "HI-AB DELIVERY REQUIRED."
            Case "axx xx": MsgBox "NO DELIVERIES BEFORE 8AM."
        End Select
    End If
End Sub
  • Related