Hello all! @rollis13 was kind enough to combine 3 VBA Worksheet_Change's for me. In particular, it had to do incorporating only certain characters in certain ranges and also make said ranges only possible to be Capital letters. Everything works but when selecting multiple ranges, I receive a Microsoft Visual Basic popup stating "Run-time error '13': Type mismatch". With the Debug function highlighting this portion of the code .Value = UCase(.Value).
If anyone can fix this, it would be greatly appreciated!
If anyone can fix this, it would be greatly appreciated!
VBA Code:
Option Explicit
Private Const FCheckRgAddress As String = "M12:CI36"
Private Const FCheckRgAddress_2 As String = "L45:T60,W45:AE60,AH45:AP60"
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xChanged As Range
Dim xRg As Range
Dim xString As String
Dim sErrors As String
Dim xRegExp As Variant
Dim xHasErr As Boolean
Set xRegExp = CreateObject("VBScript.RegExp")
xRegExp.Global = True
xRegExp.IgnoreCase = True
Set xChanged = Application.Intersect(Range(FCheckRgAddress), Target)
If xChanged Is Nothing Then GoTo second
xRegExp.Pattern = "[^A,B,C,F,I,N,O,P,S,T,X]"
For Each xRg In xChanged
If xRegExp.TEST(xRg.Value) Then
xHasErr = True
Application.EnableEvents = False
xRg.ClearContents
Application.EnableEvents = True
End If
Next
If xHasErr Then MsgBox "These cells had invalid entries and have been cleared:"
second:
Set xChanged = Application.Intersect(Range(FCheckRgAddress_2), Target)
If xChanged Is Nothing Then GoTo third
xRegExp.Pattern = "[^A,B,C,I,N,O,T,X]"
For Each xRg In xChanged
If xRegExp.TEST(xRg.Value) Then
xHasErr = True
Application.EnableEvents = False
xRg.ClearContents
Application.EnableEvents = True
End If
Next
If xHasErr Then MsgBox "These cells had invalid entries and have been cleared:"
third:
If Not (Application.Intersect(Target, Range("M12:CI36,L45:T60,W45:AE60,AH45:AP60")) Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub