Limiting text length with formulas
Video: Limiting text length with formulasIn column A of this worksheet called Text Formulas, there is a data validation rule that ensures that the Social Security Numbers are always nine characters long. That number must be that length; it can't be any more or any fewer. And that's just fine. It works great here. Notice that there's no formatting that exists here. That probably should be applied, but that is a different issue. So I'm going to put in a Social Security Number, and probably no one would ever type one like this, but you certainly could. This has nine characters, but note that it does have a letter in it.
Viewers: in countries Watching now:
In Excel 2010: Data Validation in Depth, author Dennis Taylor shows how to use the data validation tools in Excel to control how users can input data into workbooks and ensure data is entered consistently and accurately. The course covers creating dropdown lists, preventing duplicate entries, and controlling the format of numeric data, dates and times, and text entered into worksheets. Exercise files are included with the course.
- Testing for whole numbers and decimals
- Using the input message box
- Sequencing and placing lists
- Creating multi-tiered lists
- Setting date and time limitations
- Limiting text length
- Locating data validation rules
Limiting text length with formulas
In column A of this worksheet called Text Formulas, there is a data validation rule that ensures that the Social Security Numbers are always nine characters long. That number must be that length; it can't be any more or any fewer. And that's just fine. It works great here. Notice that there's no formatting that exists here. That probably should be applied, but that is a different issue. So I'm going to put in a Social Security Number, and probably no one would ever type one like this, but you certainly could. This has nine characters, but note that it does have a letter in it.
And here and there someone might see an I or zero and somehow think that might occur there, and you could certainly imagine certain kinds of code numbers where maybe it's not clear that they should be numerical, but here it is. And the data validation rule as it exists doesn't work right now. So let's change it and make sure that the entry is numerical. The data validation rule that we already have is in place, and here it is, and it's all about the Text length. If we want to use other criteria along with this, we have to change the choice here to be Custom, meaning custom formula, and there are two things we want to do here.
We want to retain this idea, so we need the And function, = And, and then the two criteria within this will be, first of all, the one related to length. You might or might not know there is a function called LEN, meaning length. You want to be sure that the length of these entries--and we'll use A1; it acts as a substitute for all cells in column A--we want to be sure that the length of A1 equals 9. That's one of our two criteria. And the other one is that we want make sure that this is a number. And sure enough, there's a function called ISNUMBER.
We want to make sure that A1 is a number. Both criteria we want to be true here. Click OK. Now data validation doesn't alter existing entries, so that's still incorrect, and we would change it manually. But let's just test this out a little bit. Let's put in something like that B entered. 9 characters, but it doesn't work. Now because we have a new data validation rule and just revisit that briefly here back to Data Validation to show it again.
Two criteria: the length must be 9 and the entry must be a number. Now you could have another kind of code, something along the lines of what you might be seeing in column B. This might be three characters, four characters, five characters, whatever. But let's say that the rule here is that this must begin with a number. And the first one does, the second one doesn't. A little bit of background in text functions might help here, and so if you had this kind of need, you might know an easy function to use is called Left.
There is a similar function called Right. This simply allows us to look at a cell and to extract the left character-- that would be 1--the two leftmost characters, a 2, and so on. So the leftmost character here, sure enough, is a 2, and if we copy the formula down to here, it's a T. Now, is this a number? Here's a function called--which we just saw--NUMBER. Is this a number? Well, it looks like a number to me.
This one doesn't; that seems to be doing the right thing, but this doesn't, so that might throw you a little bit. There is another function we need here, and it's called VALUE, and so we want to take the value of this. Is that a number? Yes, it is. So backtracking a little bit, we want to create a data validation rule here that makes sure that the leftmost character is a number, so a data validation rule here and here, too, a custom function. Equal. We want to be focused on only the leftmost character here, so L-E-F-T the left character. And once again, we use the active cell as a substitute for all the other ones. Comma 1, that means the first character only.
We want to be sure that that is a number, but we want to be looking at the value of that leftmost character. And often when you do these, of course, it's really to make a mistake regarding how many parentheses we need here. There we go. I think that looks right. Sometimes when you click OK here, you are warned that your formula is incorrect, but it could be syntactically correct and logically wrong. In this case, the syntax is correct.
Once again, existing entries are not altered. Let's see if this works. 4, 5, 6, of course that should work. And, by the way, this doesn't have any restriction of length just yet--maybe we'll do that later--but that's in place and that seems to be okay. But how about those that begin with a letter? That one is not acceptable. And as always, we might consider changing the error alert to explain what's going on. So the more you know about various Excel functions, the more likely you are to expand your use of data-validation type formulas, and in this example here, we saw how it was used to control various text-length types of entries.
There are currently no FAQs about Excel 2010: Data Validation in Depth.