Re: preventing double entry
gazzapoh, bit late replying back, but I had to go to my day job!
Anyway, to try to explain a bit:
1.
Private Sub Worksheet_Calculate()
The calculation event code is only needed for xl97, we could also add as the 1st line of code If Val(Application.Version) > 8 Then Exit Sub so it dosn't run for later versions.
2. Using the Calculation event is riddled with traps especially since your workbook may re-calculate when not the active workbook so the line: If Not Me.Parent Is ActiveWorkbook Then Exit Sub avoids any problems here.
3. Also you dont want the code to run if your sheet is not the active sheet so the next line : If Not ActiveSheet.Name = Me.Name Then Exit Sub handles this.
4. Next we check if the active cell is in column B & if it is pass the activecell to the Target range argument of the Worksheet_Change event.
5. The rest of the code in the Worksheet_Change event should work for all versions later than xl97, we just need the Calculation bit to force xl97 to play nicely!
6.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
6a)
If Not Intersect(Target, Me.[B:B]) Is Nothing Then
this line exits the code if we are not in column B
6b)
Application.EnableEvents = False
this is needed as we are going to clear the Target cell we do not wont to trigure a recursive call to the Worksheet_Change event.
6c)
For Each c In Target
If c.Column = 2 And c.Value > "" Then
we check each cell in the Target range as we dont know if the user has clicked the dropdown or typed in manually or copied a multiple cell range within our target. For each cell in column B that is not empty we then determine if it is a duplicate.
6d)
If WorksheetFunction.CountIf(Me.[B:B], c.Value) > 1 Then
i = c.Interior.ColorIndex
c.Interior.ColorIndex = 3 'red
c.Select
MsgBox "duplicate detected", vbCritical, "ERROR"
c.ClearContents: c.Interior.ColorIndex = i
We use excels CountIf function to determine if the C is a duplicate, if it is we asign the colour of the cell to i & set the C cell to red & select it & dispaly the message.
Once user closes the message we clear the cell & reset to the original colour
6e)
Application.EnableEvents = True
After we have checked all the cells within the Target we re-enable events ready for next time.
I hope this sheds some light on the basic theory for you, it is not easy to explain it breifly!