I have a monthly calendar with various assignments on it. I have code that works using Private Sub Worksheet_Change
for a single range on the calendar. This code displays a message box warning when a duplicate value [persons name] is selected for a second task in the same time period (same range of cells). I want to do this for 15 different ranges on the same worksheet. Each range should be considered independent of the other ranges. In other words the same [name] can be on a different day without any warning message box.
I found some code and modified it to do what I need and it works for the first range, but I can only have one worksheet_change
on this worksheet. I don't know how to use the same code with multiple ranges. I defined (DIM xxx as range
) for each range and SET xxx = range(ccc)
assigning the range for each variable.
How do I enable the other ranges?
Private Sub Worksheet_Change(ByVal Target As Range)
'Define your variables.
Dim Sun1AM, Sun1PM, Wed1PM As Range
Dim Sun2AM, Sun2PM, Wed2PM As Range
Dim Sun3AM, Sun3PM, Wed3PM As Range
Dim Sun4AM, Sun4PM, Wed4PM As Range
Dim Sun5AM, Sun5PM, Wed5PM As Range
'Set the range where you want to prevent duplicate entries.
Set Sun1AM = Range("C4:C14")
Set Sun1PM = Range("C17:C21")
Set Wed1PM = Range("C24:C28")
Set Sun2AM = Range("E4:E14")
Set Sun2PM = Range("E17:E21")
Set Wed2PM = Range("E24:E28")
Set Sun3AM = Range("G4:G14")
Set Sun3PM = Range("G17:G21")
Set Wed3PM = Range("G24:G28")
Set Sun4AM = Range("I4:I14")
Set Sun4PM = Range("I17:I21")
Set Wed4PM = Range("I24:I28")
Set Sun5AM = Range("K4:K14")
Set Sun5PM = Range("K17:K21")
Set Wed5PM = Range("K24:K28")
'If the cell where value was entered is not in the defined range,
'if the value pasted is larger than a single cell,
'or if no value was entered in the cell, then exit the macro.
If Intersect(Target, Sun1AM) Is Nothing Or Intersect(Target, Sun1PM) Is Nothing Or _
Intersect(Target, Wed1PM) Is Nothing Or Intersect(Target, Sun2AM) Is Nothing Or _
Intersect(Target, Sun2PM) Is Nothing Or Intersect(Target, Wed2PM) Is Nothing Or _
Intersect(Target, Sun3AM) Is Nothing Or Intersect(Target, Sun3PM) Is Nothing Or _
Intersect(Target, Wed3PM) Is Nothing Or Intersect(Target, Sun4AM) Is Nothing Or _
Intersect(Target, Sun4PM) Is Nothing Or Intersect(Target, Wed4PM) Is Nothing Or _
Intersect(Target, Sun5AM) Is Nothing Or Intersect(Target, Sun5PM) Is Nothing Or _
Intersect(Target, Wed5PM) Is Nothing Or IsEmpty(Target) _
Then Exit Sub
'If the value entered already exists in the defined range on the current worksheet, throw an
'error message.
If WorksheetFunction.CountIf(Sun1AM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun1PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Wed1PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun2AM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun2PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Wed2PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun3AM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun3PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Wed3PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun4AM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun4PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Wed4PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun5AM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Sun5PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
ElseIf WorksheetFunction.CountIf(Wed5PM, Target.Value) > 1 Then
MsgBox Target.Value & " is already used.", vbInformation, "Duplicate Entry!"
Application.EnableEvents = False
Application.EnableEvents = True
End If
End Sub
I now tried looping through all the ranges by using the variable names. I used an If statement with several Or conditions to define areas not to be considered. I used a block If - ElseIf statement to test for duplicates. None of the ranges will call up the MsgBox.
How can I make this active for all 15 ranges?
I defined (DIM xxx as range) for each range
- no you didn't. You definedWed1PM
,Wed2PM
,Wed3PM
,Wed4PM
,Wed5PM
asRange
and the rest asVariant
. – GSergWorksheet_Change
tells you which cells changed. You don't need to check the ranges the current instance ofWorksheet_Change
is not telling you about, because these have not changed since it last time fired. You explicitly want to only run the code whenTarget
is a single cell (If ... Target.Cells.Count > 1 Then Exit Sub
), so you do in fact want to only check one of the possible ranges, not all. You just need to tell which one to test, and you do that by callingApplication.Intersect
on all your Range variables until you get a match. – GSerg