Digital_Terror
n00b
- Joined
- May 29, 2014
- Messages
- 11
A spreadsheet updated by several people where I work needs to have the claim # field validated while data is being entered since it is critical to being able to look up those claims later.
There are 3 possible valid ways to enter a claim #:
First is 11 digits long, starting with an R, such as R1234567890
The 2nd and 3rd ways work for the same style claim #'s, which is 12 total digits long, starting with a 1, followed by enough 0's to make 12 total digits, such as 100001234567.
The 12 digit claim # could be entered in it's entirety by the user, or they could just enter the 1234567 portion, and the validation script needs to recognize both methods and then format the cell properly.
Here's the code I'm having troubles with:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
Application.EnableEvents = False
'Proper Case
If Not Intersect(Target, [Last_Name:First_Name]) Is Nothing Then
Target = WorksheetFunction.Proper(Target)
End If
If Not Intersect(Target, [Claim_Number]) Is Nothing Then
Select Case Target
' Check for old legacy style claim #
Case UCase(Left(Target, 1)) = "R"
If Len(Target) <> 11 Then
MsgBox ("Claim #'s starting with R must be 11 total digits.")
Else: Target = UCase(Target)
End If
' Verify valid claim # and format properly
Case (IsNumeric(Target) And Len(Target) < 12)
Target.NumberFormat = "100000000000"
Case (IsNumeric(Target) And Len(Target) = 12)
If Left(Target, 1) <> "1" Then
MsgBox ("Invalid claim #")
End If
Case Else
MsgBox ("Invalid claim #")
End Select
End If
Application.EnableEvents = True
End Sub
Note that Claim_Number is the named column range in the spreadsheet.
The portion that formats first and last names as proper case is working just fine.
The problem I am having here:
If I enter a claim # such as 'r123' it SHOULD tell me that claim #'s starting with an R must be 11 total digits...instead it just jumps straight to the Case Else message.
If I then clear the cell so that no data is in it at all, THEN it gives me the message about R claim numbers.
If I enter a valid 12 digit style claim # but use fewer than 12 digits (1234567), it formats it properly as it should...but then also tells me it is an invalid claim #.
If i enter the entire 12 digit style claim # (100001234567) it formats it so there is an extra 1 at the beginning (1100001234567) and again displays invalid claim #.
So, in effect, it seems to be executing every case option instead of just the single case option that should apply.
What am I doing wrong here?
Thanks for the assistance, this is driving me nuts!
There are 3 possible valid ways to enter a claim #:
First is 11 digits long, starting with an R, such as R1234567890
The 2nd and 3rd ways work for the same style claim #'s, which is 12 total digits long, starting with a 1, followed by enough 0's to make 12 total digits, such as 100001234567.
The 12 digit claim # could be entered in it's entirety by the user, or they could just enter the 1234567 portion, and the validation script needs to recognize both methods and then format the cell properly.
Here's the code I'm having troubles with:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
Application.EnableEvents = False
'Proper Case
If Not Intersect(Target, [Last_Name:First_Name]) Is Nothing Then
Target = WorksheetFunction.Proper(Target)
End If
If Not Intersect(Target, [Claim_Number]) Is Nothing Then
Select Case Target
' Check for old legacy style claim #
Case UCase(Left(Target, 1)) = "R"
If Len(Target) <> 11 Then
MsgBox ("Claim #'s starting with R must be 11 total digits.")
Else: Target = UCase(Target)
End If
' Verify valid claim # and format properly
Case (IsNumeric(Target) And Len(Target) < 12)
Target.NumberFormat = "100000000000"
Case (IsNumeric(Target) And Len(Target) = 12)
If Left(Target, 1) <> "1" Then
MsgBox ("Invalid claim #")
End If
Case Else
MsgBox ("Invalid claim #")
End Select
End If
Application.EnableEvents = True
End Sub
Note that Claim_Number is the named column range in the spreadsheet.
The portion that formats first and last names as proper case is working just fine.
The problem I am having here:
If I enter a claim # such as 'r123' it SHOULD tell me that claim #'s starting with an R must be 11 total digits...instead it just jumps straight to the Case Else message.
If I then clear the cell so that no data is in it at all, THEN it gives me the message about R claim numbers.
If I enter a valid 12 digit style claim # but use fewer than 12 digits (1234567), it formats it properly as it should...but then also tells me it is an invalid claim #.
If i enter the entire 12 digit style claim # (100001234567) it formats it so there is an extra 1 at the beginning (1100001234567) and again displays invalid claim #.
So, in effect, it seems to be executing every case option instead of just the single case option that should apply.
What am I doing wrong here?
Thanks for the assistance, this is driving me nuts!