Count and warn if number is exceeded

Eric Carolus

Board Regular
Joined
Sep 17, 2012
Messages
133
Office Version
  1. 2016
Platform
  1. Windows
Hi folks.
I am setting up an invigilation roster in which an educator can only be assigned once per day per room. If you attempt to assign the educator again for the same day, then Excel needs warns you and delete your second assignment of the educator. I can do this for one day, and it works for one day, but I I struggle have this done for, say, 10 days (with different dates). In other words in Range(“C5:R5”) if there is an attempt to place any teacher more than once, then Excel must warn me.
I have managed this with the code below. I wish to do the same for the ranges (C6:R6, C7:R7, C8:R8), etc.

The abbreviations on the right are the codes referring to teachers. The numbers on the RIGHT simply refers to the total of times the teacher had been assigned on the whole grid.
My code for the topmost row (C5:R5), as follows:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim iVal As Integer 'Correcct
Dim myRange As Range 'Correct


Set myRange = Application.ActiveWorkbook.Sheets("CountPeriods").Range("C5:R5")


iVal = Application.WorksheetFunction.CountIf(myRange, "AC") Or Application.WorksheetFunction.CountIf(myRange, "ZAM") Or Application.WorksheetFunction.CountIf(myRange, "ZS") _
Or Application.WorksheetFunction.CountIf(myRange, "SF") Or Application.WorksheetFunction.CountIf(myRange, "LJ") Or Application.WorksheetFunction.CountIf(myRange, "NL") _
Or Application.WorksheetFunction.CountIf(myRange, "LM") Or Application.WorksheetFunction.CountIf(myRange, "AMJ") Or Application.WorksheetFunction.CountIf(myRange, "AMV") _
Or Application.WorksheetFunction.CountIf(myRange, "VN") Or Application.WorksheetFunction.CountIf(myRange, "MN") Or Application.WorksheetFunction.CountIf(myRange, "NQ") _
Or Application.WorksheetFunction.CountIf(myRange, "SR") Or Application.WorksheetFunction.CountIf(myRange, "GS") Or Application.WorksheetFunction.CountIf(myRange, "DS") _
Or Application.WorksheetFunction.CountIf(myRange, "CS") Or Application.WorksheetFunction.CountIf(myRange, "PS") Or Application.WorksheetFunction.CountIf(myRange, "JT") _
Or Application.WorksheetFunction.CountIf(myRange, "MY")


If iVal > 1 Then
On Error Resume Next
MsgBox "This teacher had ALREADY been entered once!"
Selection.Offset(0, -1).ClearContents
Selection.Offset(0, -1).Select
Exit Sub
End If

End Sub

Please help.

Thanks in advance.
 
Hi Folks

My thanks to Michael M as well!

I now now how to insert a link
from Dropbox.

Thank you.

Eric
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,223,606
Messages
6,173,323
Members
452,510
Latest member
RCan29

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