问题
I know excel "programming" is not very popular among fellow programmers, however I've been struggling to get this right and management is on my neck..
I have the custom validation on excel :
=AND(LEN(AV15)=10,((VALUE(LEFT(AV15,2)))<=31),NOT(ISERROR(VALUE(LEFT(AV15,2)))),MID(AV15,3,1)="/",((VALUE(MID(AV15,4,2)))<=12),NOT(ISERROR(VALUE(MID(AV15,4,2)))),MID(AV15,6,1)="/",((VALUE(RIGHT(AV15,4)))<=2100),NOT(ISERROR(VALUE(RIGHT(AV15,4)))))
The validation above is supposed to accept any valid date in the format:
dd/mm/yyyy
It seems to be working partially, but somehow it wont accept a day lower than "12", example:
14/12/2010 -->accepted
13/10/2010 -->accepted
25/10/2010 -->accepted
12/10/2010 -->gives error
At first glance one would thing that the ((VALUE(MID(AV15,4,2)))<=12)
is causing this behavior, but I changed it to 31 and I still get the error, I need the validation to admit inputs in :
nn/nn/nnnn
where "n" is a number, i don't care if they input 99/99/9999 I can check that later on vba code, but the input has to specifically have the 10 characters.
any help would be highly appreciated
回答1:
I put your formula in and when you put a '
before the date it works just fine. What is going on is that when you put any value below 13
as the day it evaluates the date like a date
, which is stored as a number
in Excel. When you have above 13
it evaluates as a string
. So what you need to do is format the cell to Text
format. Then it should work just fine.
回答2:
To see if the following works, put the date value in A1
A1 -> '14/12/2010
Put the following formulas as mentioned below
B1 -> =MID(A1,3,1) = "/"
C1 -> =MID(A1,6,1) = "/"
D1 -> =IFERROR(AND(VALUE(MID(A1,4,2)) >= 1, VALUE(MID(A1,4,2)) <= 12), FALSE)
E1 -> =AND(VALUE(RIGHT(A1,4))>=2000,AND(VALUE(RIGHT(A1,4))<=2100))
F1 -> =AND(B1,C1,D1, E1)
The formulas above are splitted and you will have to combine them (as it is done in cell F1).
Hope that helps.
EDIT: The combined formula for validation will be (note that I have used A1 as the cell)
=AND(MID(A1,3,1) = "/", MID(A1,6,1) = "/", IFERROR(AND(VALUE(MID(A1,4,2)) >= 1, VALUE(MID(A1,4,2)) <= 12), FALSE), AND(VALUE(RIGHT(A1,4))>=2000,AND(VALUE(RIGHT(A1,4))<=2100)))
回答3:
Can you not simply choose the correct format in the cell?
- Right click in the cell and select 'Format Cell'
- Select 'Custom' from the left side navigation
- In the 'Type:' box enter this: dd/mm/yyyy
回答4:
Excel appears to be recognising values such as 10/12/2010
as dates and storing them internally in a different format, such as the number of days since Jan 1 1900 or suchlike. It happens that when you use LEFT
, MID
and RIGHT
on these date values, Excel doesn't do the conversion back from its internal format. In particular, I put 10/12/2010
in cell A1 and =LEFT(A1,10)
in cell B1. Cell B1 then showed me the value 40522
. (I'm in the UK and using Excel 2010 Starter. You may get different values in other locales or with other versions of Excel.)
Try replacing all occurrences of AV15
in your formula with TEXT(AV15, "DD/MM/YYYY")
. Alternatively, put =TEXT(AV15, "DD/MM/YYYY")
in another cell and use this other cell in your formula in place of AV15
.
回答5:
If you aren't wary of VBA, then what about a simple UDF?
Public Function DateFormat(rng As Range)
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "\d\d/\d\d/\d\d\d\d"
test = regEx.Execute(rng.Value).Count > 0
End Function
This should return true if the value of a cell matches nn/nn/nnnn where n is any number.
Then you could simply say =DateFormat(AV15)
来源:https://stackoverflow.com/questions/8605804/this-custom-validation-is-not-working-but-i-dont-know-whats-happenning