To enforce the conditions you mentioned using VBA, you can use the Worksheet_Change event and relevant functions to check and modify the values entered in column B. Here's an example code that can be added to the VBA module of the worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim cell As Range
' Specify the range of column B where the data validation is applied
Set rng = Range("B2:B" & Me.Cells(Me.Rows.Count, "B").End(xlUp).Row)
' Check if the changes were made in the target range
If Not Intersect(Target, rng) Is Nothing Then
Application.EnableEvents = False ' Disable events to prevent recursive triggering
For Each cell In Intersect(Target, rng)
' Convert entered value to uppercase
cell.Value = UCase(cell.Value)
' Remove numbers and symbols from the value
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "0", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "1", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "2", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "3", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "4", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "5", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "6", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "7", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "8", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "9", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "!", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "@", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "#", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "$", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "%", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "^", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "&", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "*", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "(", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ")", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "-", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "_", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "=", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "+", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "[", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "{", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "]", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "}", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "\", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "|", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ";", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ":", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "'", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, """", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ",", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "<", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ".", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, ">", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "/", "")
cell.Value = Application.WorksheetFunction.Substitute(cell.Value, "?", "")
' Check if the modified value already exists in column B
If Application.WorksheetFunction.CountIf(rng, cell.Value) > 1 Then
cell.ClearContents
MsgBox "Duplicates are not allowed in column B.", vbCritical, "Duplicate Value"
cell.Select
End If
Next cell
Application.EnableEvents = True ' Re-enable events
End If
End Sub
To implement this code:
- Open the VBA editor by pressing
Alt + F11.
- In the VBA Project window, expand the "Microsoft Excel Objects" folder and double-click on the worksheet where you want to apply these validations.
- Paste the above code into the code window that opens.
- Save the workbook as a macro-enabled workbook (*.xlsm) if it isn't already saved in that format.
Now, when someone enters or modifies a value in column B, the code will automatically convert it to uppercase, remove numbers and symbols, and check for duplicates. If a duplicate is found, a message box will appear indicating that duplicates are not allowed, and the entered value will be cleared.