Custom Format?

paulstan

Board Regular
Joined
Mar 12, 2011
Messages
85
I need a column to accept one of the following entries only:
  • be left blank
  • date (in dd/mm/yyyy) format
  • or the word 'Validated'
If anything else is input, then an error message is displayed. Am I able to do this through a custom format, or will it require VBA?

Regards

Paul S
 
@Paul,

Okay, I see the problem. We need to use two event procedures to get around this problem. Replace the Change event code procedure you are now using with the following two event code procedures...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range, CellText As String
    If Not Intersect(Target, Columns("A")) Is Nothing Then
    For Each Cell In Target
        CellText = UCase(Cell.Text)
        If Len(CellText) = 0 Then Exit Sub
        If CellText = "NO CLAIM" Then
            Application.EnableEvents = False
            Cell.Value = "Validated"
            Application.EnableEvents = True
            Exit Sub
        ElseIf IsDate(CellText) And CellText Like "*[!0-9]*" Then
            Application.EnableEvents = False
            Cell.Value = CDate(CellText)
            Application.EnableEvents = True
            Cell.NumberFormat = "dd/mm/yyyy"
            Exit Sub
        End If
        MsgBox "                 Invalid Entry!" & vbCr & "Please enter one of the following: " & vbCrLf & vbCrLf & "type the text 'no claim'" & vbCr & "date (in dd/mm/yyyy format)" & vbCr & "leave blank"
        Target.Select
    Next
    End If
End Sub
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
    If Not Intersect(Target, Columns("A")) Is Nothing Then
        For Each Cell In Target
            Target.NumberFormat = "General"
        Next
    End If
End Sub
Note: I included the change you made from "Validated" to "No Claim" and I also included your current MessageBox message in this code. In addition, I added code to stop the error that gets generated if you try to delete multiple entries.
 
Last edited:
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Rick

Many thanks for that. Unfortunately, when you type in a valid date, leave the cell and then return to it, the date changes to its numerical value; today's date: 15/07/2011 is showing as 40793.

Regards

Paul S
 
Upvote 0
Further explanation!
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p>All credit for the coding below goes to Rick Rothstein</o:p>
<o:p></o:p>
I am trying to restrict a column to any of the 3 below which can be entered by the user:

<o:p></o:p>
  • left blank
  • the word ‘Validated’
  • or a date entered by the user in the format dd/mm/yyyy
If anything other than the above is entered, an error message appears for the user to try again.
<o:p></o:p>
<o:p>First Code Block</o:p>
<o:p></o:p>
<o:p>
Code:
<o:p> 
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range, CellText As String
    If Not Intersect(Target, Columns("A")) Is Nothing Then
    For Each Cell In Target
        CellText = UCase(Cell.Text)
        If Len(CellText) = 0 Then Exit Sub
        If CellText = "VALIDATED" Then
            Application.EnableEvents = False
            Cell.Value = "Validated"
            Application.EnableEvents = True
            Exit Sub
        ElseIf IsDate(CellText) And CellText Like "*[!0-9]*" Then
            Application.EnableEvents = False
            Cell.Value = CDate(CellText)
            Application.EnableEvents = True
            Cell.NumberFormat = "dd/mm/yyyy"
            Exit Sub
        End If
        MsgBox "Invalid Entry!" & vbCr & "Please enter one of the following: " & vbCrLf & vbCrLf & "type the text 'validated'" & vbCr & "date (in dd/mm/yyyy format)" & vbCr & "leave blank"
        Target.Select
    Next
    End If
End Sub
</o:p>
</o:p><o:p></o:p>
When I use the first code block I get the following problem:
<o:p></o:p>
A user can type in the word validated, which works correctly. The problem is with dates. A user can enter a correct date, ie <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:date Month="7" Day="18" Year="2011">18/07/2011</st1:date>, which will show correctly; however, the user can over-write this cell with a number, for instance 111 and the date will change to <st1:date Month="4" Day="20" Year="1900">20/04/1900</st1:date>. If they try to over-write with text, then the error message appears.
<o:p></o:p>
So, the second code block was added and the following problem now occurs, still with dates. A user can enter a date, ie <st1:date Month="7" Day="18" Year="2011">18/07/2011</st1:date>. Upon leaving the cell everything appears correct; however, when you revisit the cell or roll-over it the date changes to its numerical value, in this case 40742.

Second Code Block

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cell As Range
    If Not Intersect(Target, Columns("A")) Is Nothing Then
        For Each Cell In Target
            Target.NumberFormat = "General"
        Next
    End If
End Sub

Regards

Paul S
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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