- what is meaning of "*" and "--" in the formula
=FIND(LEFT(A1),Letters)*(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
The formula is a combination of 2 separate tests:
FIND(LEFT(A1),Letters)
(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
The first test checks that the first character in the string is an uppercase
letter. If it is it returns that letter's position where it's found in the
named string Letters. For example, if the cell contained:
D100
Then the uppercase letter D is found at position 4 of the named string
Letters. So:
FIND(LEFT(A1),Letters) = 4
The second test checks to make sure the next 3 characters in the string D100
are the digits 0-9.
--MID(A1,2,3)
Starting from the 2nd character in the string D100, return the next 3
characters = "100". The MID function *always* returns a TEXT value even if
it looks like a number. There is a difference in the data types TEXT and
NUMBER. Excel treats those data types differently. So, we need to convert
the TEXT "100" to the numeric number 100. One way to do that is to use the
double unary minus --.
--"100" = 100
The first - converts the TEXT string "100" to a negative number:
-"100" = -100
The second - then converts the negative number back to a positive number:
--100 = 100
One possible problem with this is that Excel doesn't recognize leading 0s as
part of a numeric number. For example, if you try to enter the number 001 in
a cell Excel will automatically strip off the leading 0s so the cell entry
will be just the number 1.
This comes into play if your cell entry to be validated was D001:
MID(A1,2,3) = "001" but:
--MID(A1,2,3) will strip off those leading 0s leaving us with the single
number 1.
That's why we use the TEXT function:
TEXT(--MID(A1,2,3),"000")
The TEXT function returns the numeric number we extracted with the MID
function as a TEXT number in the format 000. This in effect will replace any
leading 0s that may have been stripped off by --MID(A1,2,3).
I'm pretty sure that by now you're getting confused!
Just hang in there!!!
So, if the cell entry was D001 then:
TEXT(--MID(A1,2,3),"000") = "001"
We now compare that result to MID(A1,2,4):
(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
MID(A1,2,4):
Starting from the 2nd character in the string D001, return the next *4*
characters. Hmmm... there are only 3 characters! So why do you want to
return 4 characters?
If the string was D1234 that's too many characters since a valid entry must
have only 4 characters. That will cause this to fail the test:
(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
"1234" = "123" = FALSE
In effect, this not only tests that the next 3 characters are the numbers
0-9 but it also acts as test to make sure the total length of the string is
4 characters.
MID(A1,2,4) will return up to the next 4 characters starting from the 2nd
character. If there aren't 4 characters it'll return whatever number of
characters are there:
D1234 = "1234"
D123 = "123"
D12 = "12"
D1 = "1"
D = ""
The only time the test will pass is when there there are *only* 3 numbers:
"1234" = "123" = FALSE
"123" = "123" = TRUE
"12" = "012" = FALSE
"1" = "001" = FALSE
"" = "000" = FALSE
So, the test for the 3 digits 0-9 will return either TRUE or FALSE. If the
cell entry was:
D123
Then:
FIND(LEFT(A1),Letters) = 4
(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000")) = TRUE
We then multiply these resutls:
=FIND(LEFT(A1),Letters)*(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
4*TRUE = 4
Whenever the result of this multiplication is **any number other than 0**
then Excel evaluates that as being TRUE and passes the validation test
allowing the cell entry. If the cell entry was D1234 then:
4*FALSE = 0
When the first test fails:
FIND(LEFT(A1),Letters)
FIND will return a #VALUE! error and cause the entire formula:
=FIND(LEFT(A1),Letters)*(MID(A1,2,4)=TEXT(--MID(A1,2,3),"000"))
To return the #VALUE! error and therefore fail the test and not allow the
cell entry.
OK, your next question was:
- by using the formula of Luke M, why only
A1e1 returns "True", and other letter, like
A1b1 will return "False"
1e1 is allowed because Excel evaluates 1e1 as a number in scientific
notation. 1e1 or 1E1 is scientific notation for the number 10. Try typing
1e1 into a cell and see what happens. Excel will convert that into the
number 1.00E+01. Change the cell format to General and it will now display
the number 10.
So, in Luke's formula:
1e1 passes the test:
ISNUMBER(--RIGHT(F3,3))