Melowshipslinki
New Member
Now that my Worksheet Change event set up, so that when a certain phrase 'x' is entered into any cell in Range("H:H"), a MsgBox appears, saying, "the use of 'x' is forbidden; use 'y' instead". This is so that my users are made aware of the fact that 'x' is invalid, and so they develop a habit of using the valid phrase.
I would like to be able to include multiple constants, forbidden phrases specifically. I have uploaded a blank spreadsheet with my code in Sheet1. For some reason that I'm not aware of yet, I can't create and run one macro without having another empty macro below it (another problem for another thread), but that's why there's an empty macro in my sample :/
I've tried all kinds of syntax in the code,
Const ksForbidden = "x","x1"
Const ksForbidden = "x" + "x1"
etc.
Here's what I have now, it doesn't work now- am I at least on the right track with adding an ElseIf? Or should there be a second With instead? From the examples I looked at, I'm pretty sure there can't be two separate Worksheet Change events in one WorkBook, unless I understood wrong. Any help would be greatly appreciated!!!
Private Sub Worksheet_Change(ByVal Target As Range)
Const ksTestRange = "H:H"
Const ksForbidden = "x"
Const ksForbidden1 = "x1"
Const ksAllowed = "y"
If Application.Intersect(Range(ksTestRange), Target) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target
If .Value Like "*" & ksForbidden & "*" Then
MsgBox "Use of <" & ksForbidden & "> is forbidden; use <" & _
ksAllowed & "> instead.", _
vbApplicationModal + vbExclamation + vbOKOnly, "Warning"
ElseIf .Value Like "*" & ksForbidden1 & "*" Then
MsgBox "Use of <" & ksForbidden1 & "> is forbidden; use <" & _
vbApplicationModal + vbExclamation + vbOKOnly, "Warning"
End If
End If
End With
End Sub
I would like to be able to include multiple constants, forbidden phrases specifically. I have uploaded a blank spreadsheet with my code in Sheet1. For some reason that I'm not aware of yet, I can't create and run one macro without having another empty macro below it (another problem for another thread), but that's why there's an empty macro in my sample :/
I've tried all kinds of syntax in the code,
Const ksForbidden = "x","x1"
Const ksForbidden = "x" + "x1"
etc.
Here's what I have now, it doesn't work now- am I at least on the right track with adding an ElseIf? Or should there be a second With instead? From the examples I looked at, I'm pretty sure there can't be two separate Worksheet Change events in one WorkBook, unless I understood wrong. Any help would be greatly appreciated!!!
Private Sub Worksheet_Change(ByVal Target As Range)
Const ksTestRange = "H:H"
Const ksForbidden = "x"
Const ksForbidden1 = "x1"
Const ksAllowed = "y"
If Application.Intersect(Range(ksTestRange), Target) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target
If .Value Like "*" & ksForbidden & "*" Then
MsgBox "Use of <" & ksForbidden & "> is forbidden; use <" & _
ksAllowed & "> instead.", _
vbApplicationModal + vbExclamation + vbOKOnly, "Warning"
ElseIf .Value Like "*" & ksForbidden1 & "*" Then
MsgBox "Use of <" & ksForbidden1 & "> is forbidden; use <" & _
vbApplicationModal + vbExclamation + vbOKOnly, "Warning"
End If
End If
End With
End Sub