How do I code this...

mrkris1982

Active Member
Joined
Apr 16, 2009
Messages
407
Column A is a validation drop down menu where you pick a name. Lets say we choose Jack Smith (CTB)....how would I go about making a message pop up whenever (CTB) is behind someones name?

I'd just like a message like:
"1) Check Access List 2) Sign key out in binder"

Thank you.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Have a look at the Worksheet_Change function in VBA. You could check what cell has been changed and then check for the last 5 charatcers in the cell being (CTB).
If they are, you could then display a messagebox.
 
Upvote 0
I know how to look at the code...but I really dont know how to do what you said.

Its under Sheet2(Data Center Access Log). When i double click it, this is all the code that is there right now:

Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.CalculateFull
End Sub

Not sure if that helps any, but I am pretty much clueless coming up with any VBA on my own.
 
Upvote 0
It's not the selectionchange event you want, it just the change event. In the dropdown in the top-right corner is a list of all the automatic things Excel can check for. Have a look in there for the change event.
Target will be the address of the cell you're checking for or the one that's changed.
Something like:-
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        If Right(Target.Value, 5) = "(CTB)" Then
            MsgBox "1: Check Access List " & Chr(10) & _
                "2: Sign out in binder"
        End If
    End If
End Sub
You'll need to change the cell address to whatever you need it to be.
 
Upvote 0
I selected Change in the dropdown and pasted your code with minor changes...

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "A1:A10000" Then
        If Right(Target.Value, 5) = "(CTB)" Then
            MsgBox "1: Check Access List " & Chr(10) & _
                "2: Sign out in binder"
        End If
    End If
End Sub

I then went to the main worksheet, chose a name that has (CTB) behind their name and nothing happened.....i must be doing something wrong!
 
Upvote 0
Target.Address is returning a single string value like $A$10 or $A$30. Since you are testing a string against a string, it won't fire properly. For a Worksheet_Change macro, I suggest using the If Not Intersect(Target, Range()) Is Nothing format:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
    If Right(Target.Value, 5) = "(CTB)" Then
        MsgBox "1: Check Access List " & vbLf & _
            "2: Sign out in binder"
    End If
End If
End Sub
 
Upvote 0
BINGO!! That worked great...thank you!

This code seems like it is looking at the last 5 characters. Is there a way to check the entire string for something or does the character placements have to be specific? This works, but I have others with multiple things behind the name.

Thanks!
 
Upvote 0
Actually, if I want to add more of these types of statements, how do I go about doing it...I tried the following, to no avail:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
    If Right(Target.Value, 5) = "(CTB)" Then
        MsgBox "1: Check Access List " & vbLf & _
            "2: Sign out in binder"
    End If
End If
End Sub
Private Sub Worksheet_Change2(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
    If Right(Target.Value, 6) = "?cable" Then
        MsgBox "1: What are your intentions with the server cabinet? " & vbLf & _
            "2: Are you going to work with cabling?" & vbLf & _
            "3: If YES, have you contacted the Infrastructure Support Team?"
    End If
End If
End Sub
 
Upvote 0
The problem seems to the there are two of these routines:
Code:
Private Sub Worksheet_Change2(ByVal Target As Range)

What lines do I need to delete in the second routine in order to add it as a continuation off the first routine?
 
Upvote 0
Put them both into a single event:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
    If InStr(Target.Value, "(CTB)") > 0 Then
        MsgBox "1: Check Access List " & vbLf & _
            "2: Sign out in binder"
    ElseIf InStr(Target.Value, "?cable") > 0 Then
        MsgBox "1: What are your intentions with the server cabinet? " & vbLf & _
            "2: Are you going to work with cabling?" & vbLf & _
            "3: If YES, have you contacted the Infrastructure Support Team?"
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top