Excel - Restrict entry in specific cols - Dates

Hi,
everyone.
In excel how do we restrict the dates being entered to a specific column, to 1/1/1900 and between 1/1/1970 to 1/12/2007. This should not apply to row 1.
:wave:
[185 byte] By [VBFnewcomer] at [2007-12-5 11:57:20]
# 1 Re: Excel - Restrict entry in specific cols - Dates
you could use the prev range from your other thread to validate the date at the same time, when you leave the cell
try something like
if prev.column = datecol then
if prev.value > "1/12/2007" or prev.value < "1/1/1970"
westconn1 at 2007-12-6 11:45:54 >
# 2 Re: Excel - Restrict entry in specific cols - Dates
datecol what is this some constant?
VBFnewcomer at 2007-12-6 11:46:54 >
# 3 Re: Excel - Restrict entry in specific cols - Dates
just the column number you want to check the date
westconn1 at 2007-12-6 11:47:55 >
# 4 Re: Excel - Restrict entry in specific cols - Dates
do we need to manually provide the col num. can't we do it automatically something like if col_type = date then
VBFnewcomer at 2007-12-6 11:49:00 >
# 5 Re: Excel - Restrict entry in specific cols - Dates
try
if prev.numberformat = ' will have to match your set date format
westconn1 at 2007-12-6 11:49:59 >
# 6 Re: Excel - Restrict entry in specific cols - Dates
Like this
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not prev Is Nothing Then
If Not prev.Row = 1 Then
prev.Value = UCase(prev.Value)
If prev.NumberFormat = "dd/mm/yyyy" Then 'format of date in the column
If prev.Value <> "1/1/1900" Then
If prev.Value > "1/12/2007" Or prev.Value < "1/1/1970" Then
MsgBox "invalid value"
End If
End If
End If
End If
End If
Set prev = Target
End Sub
I am doing one or more things wrong. It is not working.
VBFnewcomer at 2007-12-6 11:50:58 >
# 7 Re: Excel - Restrict entry in specific cols - Dates
on testing i found 2 problems,
1. prev.Value = UCase(prev.Value), the date i had in a cell 01/12/2007 (formatted dd/mm/yyyy) changed on the above line to 12/01/2007, probably the safest solution is to only ucase as an else to if the number format matches

2 your or should probably be and
edit: ignore this second one, i think it is incorrect
westconn1 at 2007-12-6 11:52:01 >
# 8 Re: Excel - Restrict entry in specific cols - Dates
If prev.NumberFormat = "dd/mm/yyyy" Then in break mode the number format shows (tooltip) "dd\/mm\/yyyy" ? and skips the code inside the if..else
probably the safest solution is to only ucase as an else to if the number format matches do you mean like this
If prev.NumberFormat = "dd/mm/yyyy" Then
If prev.Value <> "1/1/1900" Then
If prev.Value > "1/12/2007" Or prev.Value < "1/1/1970" Then
MsgBox "invalid value"
End If
End If
Else
prev.Value = UCase(prev.Value)
End If
VBFnewcomer at 2007-12-6 11:52:56 >
# 9 Re: Excel - Restrict entry in specific cols - Dates
i think the code is correct, you need to fix the numberformat of your date column, don't know how you can get that format
easiest way is to set as custom format then put the dd/mm/yyyy, as that format was not available for me to select in the dates, even though it is my system locale date format
westconn1 at 2007-12-6 11:54:03 >
# 10 Re: Excel - Restrict entry in specific cols - Dates
Hi

As far as manually doing it, I have already replied to your pm...

In the above code, you need to first decide what date format are you going to use... dd/mm/yyyy or d/m/yyyy

and when you compare date values then you need to do something like this..

If Format(prev.Value, "dd/mm/yyyy") <> "01/01/1900" Then
'Similarly
If Format(prev.Value, "dd/mm/yyyy") > "01/12/2007" Or Format(prev.Value, "dd/mm/yyyy") < "01/01/1970" Then

Also Ucase is not used for 'dates'. The function is used to convert the case of text to UPPERCASE for example

txt = "aaa"
MsgBox UCase(txt) 'will give you "AAA"

Hope this helps..
koolsid at 2007-12-6 11:55:04 >
# 11 Re: Excel - Restrict entry in specific cols - Dates
you need to first decide what date format are you going to use... dd/mm/yyyy or d/m/yyyy
I did set the format for the reqd cols as "dd/mm/yyyy" using the format->cells..
in break mode the number format shows (tooltip) "dd\/mm\/yyyy" ? and skips the code inside the if..else what is happening
koolsid your code is for checking each value being entered. The problem here is based on the format of a col.
We need to first check if the col is date col (formatted for date)
Then we need to check if the value is/or not equal to 01/01/1900
If not then check if the value is between 01/01/1970 and 30/11/2007
If both the check fails the the value is illegal, inform the user (say msgbox) and bring the focus back to the same cell and set the default date as 01/01/1900.
It should however allow the cell being left blank.
I did see your PM. but the problem is we are checking against more than 1 values ranges. 01/01/1900, between 01/01/1970 and 30/11/2007. Thus apparently this checking is not possible to be accomplished :afrog:
VBFnewcomer at 2007-12-6 11:56:05 >
# 12 Re: Excel - Restrict entry in specific cols - Dates
why not just check the date in specific columns, which hold the dates
i do not understand why your numberformat is not returned as dd/mm/yyyy if that is what you set it to, how exactly are you setting you date format?, format cells or by code?
westconn1 at 2007-12-6 11:57:02 >
# 13 Re: Excel - Restrict entry in specific cols - Dates
i do not understand why your numberformat is not returned as dd/mm/yyyy me too :D
how exactly are you setting you date format?Format>Cells..>Category(custom)>Type(dd/mm/yyyy).
I message boxed it (in sheet change event) still show dd\/mm\/yyyy:eek:
VBFnewcomer at 2007-12-6 11:58:09 >
# 14 Re: Excel - Restrict entry in specific cols - Dates
yeah that is exactly how i set the number format, but mine returns correctly
dd/mm/yyyy, must be a language thing i guess, try setting a range by code instead, see if it is correct then
range("r1:r20").numberformat = "dd/mm/yyyy"
westconn1 at 2007-12-6 11:59:04 >
# 15 Re: Excel - Restrict entry in specific cols - Dates
bear with me where do I specify the range that is in which event of the sheet
VBFnewcomer at 2007-12-6 12:00:09 >
# 16 Re: Excel - Restrict entry in specific cols - Dates
I also noticed that using numberformatLocal(in code) shows the correct format but I unable to understand the diff between numberformat & numberformatLocal or its significance
VBFnewcomer at 2007-12-6 12:01:13 >
# 17 Re: Excel - Restrict entry in specific cols - Dates
well if that works for you, use that
westconn1 at 2007-12-6 12:02:10 >
# 18 Re: Excel - Restrict entry in specific cols - Dates
well if that works for you, use that
diff between numberformat & numberformatLocal or its significance
VBFnewcomer at 2007-12-6 12:03:14 >
# 19 Re: Excel - Restrict entry in specific cols - Dates
I dont suppose you want me to blindly use it?
VBFnewcomer at 2007-12-6 12:04:12 >
# 20 Re: Excel - Restrict entry in specific cols - Dates
bad news. I started a macro. After setting the format of date from Custom I got this code in the module Columns("E:E").Select
Selection.NumberFormat = "dd\/mm\/yyyy" :eek: :eek:
VBFnewcomer at 2007-12-6 12:05:16 >
# 21 Re: Excel - Restrict entry in specific cols - Dates
looks to me as if it is you format cells option that is putting in the incorrect format, i think it must be putting in the backslash charater to escape the slash, which it must require as part of the language for the locale, that is the difference between numberformat and numberformatlocal

an example is :- similar to putting quotes in strings
as my computer only has english language installed it is hard for me to be more certain about what is happening
westconn1 at 2007-12-6 12:06:12 >
# 22 Re: Excel - Restrict entry in specific cols - Dates
mine is also Eng. but the Regional settings set to Indian.
Do I take it numberformatLocal can be safely used in all places to check the formats...
VBFnewcomer at 2007-12-6 12:07:10 >