Another Conditional Error Message

bhath2

New Member
Joined
Dec 8, 2005
Messages
7
The macro below creates an error message when an entry is in both columns (P & Q) of a given row. I would like to do the same thing as this, but, instead, if there is an entry in column Q, there MUST be an entry in column R (and vice versa). Just like this existing macro, I would like for it to automatically select the offending cell, but I dont want it to delete the contents of the cell (because its already empty, of course). I suspect that all of this can be accomplished with slight changes to the existing macro, but I'm not sure how to do it. Any help is appreciated.

_____________________________________________________________

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range, myMsg As String

'if more than 1 cell is changed, exit macro
If Target.Count > 1 Then Exit Sub

'if the changed cell was not in P2:Q1000, exit macro
If Intersect(Target, [P2:Q1000]) Is Nothing Then Exit Sub

'if the new value of the changed cell is blank, exit macro
If Target.Value = "" Then Exit Sub

'otherwise

Application.EnableEvents = False

'detect the column of the changed cell
Select Case Target.Column
Case Is = 16 'column P
Set myRng = Target.Offset(, 1) 'sets cell in column Q as range
myMsg = "Complaint Issue."
Case Is = 17 'column Q
Set myRng = Target.Offset(, -1) 'sets cell in column P as range
myMsg = "NCMR Issue."
End Select

'if the value of the cell opposite the changed cell is not blank
If myRng <> "" Then
With Target
.ClearContents 'delete value entered into changed cell
.Select 'select the changed cell
End With
'display message
MsgBox "There is already an entry in " & myMsg & " An entry can be made as an NCMR or Complaint but not both.", vbOKOnly + vbCritical, "Duplicate Entry"
End If

Application.EnableEvents = True

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Something like?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range, myMsg As String

'if more than 1 cell is changed, exit macro
If Target.Count > 1 Then Exit Sub

'if the changed cell was not in columns Q:R, exit macro
If Intersect(Target, Columns("Q:R")) Is Nothing Then Exit Sub

'if the new value of the changed cell is blank, exit macro
If Target.Value = "" Then Exit Sub

'otherwise

Application.EnableEvents = False

'detect the column of the changed cell
Select Case Target.Column
    Case Is = 17 'column Q
    Set myRng = Target.Offset(, 1) 'sets cell in column R as range
    myMsg = "Complaint Issue."
Case Is = 18 'column R
    Set myRng = Target.Offset(, -1) 'sets cell in column Q as range
    myMsg = "NCMR Issue."
End Select

'if the value of the cell opposite the changed cell is not blank
If myRng = "" Then
    Target.Select 'select the changed cell
    'display message
    MsgBox "There is no entry in " & myMsg & _
        " An entry must be made in both.", _
        vbOKOnly + vbCritical, "Duplicate Entry"
End If

Application.EnableEvents = True

End Sub
 
Upvote 0
Barrie,
The code you gave above works great if Q contains a value and R is blank, but it doesn't do anything if R contains a value and Q is blank. Any ideas why?

I've also tried duplicating it for other columns in my sheet (S and T) and it didnt work there either. I don't know if it makes a difference but I've put the two sets of code (from the previous responses above) together in the Worksheet's code, so it looks like this:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range, myMsg As String

'NCMR_vs_Complaint_Error_Message_Macro
'if more than 1 cell is changed, exit macro
If Target.Count > 1 Then Exit Sub

'if the changed cell was not in P2:Q1000, exit macro
If Intersect(Target, [P2:Q1000]) Is Nothing Then Exit Sub

'if the new value of the changed cell is blank, exit macro
If Target.Value = "" Then Exit Sub

'otherwise

Application.EnableEvents = False

'detect the column of the changed cell
Select Case Target.Column
Case Is = 16 'column P
Set myRng = Target.Offset(, 1) 'sets cell in column Q as range
myMsg = "Complaint Issue."
Case Is = 17 'column Q
Set myRng = Target.Offset(, -1) 'sets cell in column P as range
myMsg = "NCMR Issue."
End Select

'if the value of the cell opposite the changed cell is not blank
If myRng <> "" Then
With Target
.ClearContents 'delete value entered into changed cell
.Select 'select the changed cell
End With
'display message
MsgBox "There is already an entry in " & myMsg & " An entry can be made as an NCMR or Complaint but not both.", vbOKOnly + vbCritical, "Duplicate Entry"
End If

Application.EnableEvents = True


'Complaint_must_include_Customer_Macro
'if more than 1 cell is changed, exit macro
If Target.Count > 1 Then Exit Sub

'if the changed cell was not in columns Q:R, exit macro
If Intersect(Target, [Q2:R1000]) Is Nothing Then Exit Sub

'if the new value of the changed cell is blank, exit macro
If Target.Value = "" Then Exit Sub

'otherwise

Application.EnableEvents = False

'detect the column of the changed cell
Select Case Target.Column
Case Is = 17 'column Q
Set myRng = Target.Offset(, 1) 'sets cell in column R as range
myMsg = "the 'Customer' column."
Case Is = 18 'column R
Set myRng = Target.Offset(, -1) 'sets cell in column Q as range
myMsg = "Complaint Issue."
End Select

'if the value of the cell opposite the changed cell is not blank
If myRng = "" Then
Target.Select 'select the changed cell
'display message
MsgBox "There is no entry in " & myMsg & _
" Please be sure to indicate the customer for this complaint.", _
vbOKOnly + vbCritical, "Duplicate Entry"
End If

Application.EnableEvents = True

End Sub
____________________end code

Again, the first part "NCMR_vs_Complaint_Error_Message_Macro" works fine, but only half of "Complaint_must_include_Customer_Macro" works. Please help. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,590
Members
449,039
Latest member
Arbind kumar

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