Cellgrids Cellgrids
Home › Practice questions › AND › Question 7

Excel AND Practice Question: A code is valid if it starts with HR, has exactly 7 characters and ends with 4…

By Rinkesh Shah, founder of Cellgrids.

The question

A code is valid if it starts with HR, has exactly 7 characters and ends with 4 digits. Write Valid or Invalid in B2 to B4.

The data

AB
1CodeCheck
2HR-2041?
3HR-20A1?
4FIN-205?

The yellow cells are where your answer goes.

Show answer

Expected results:

  • B2 = Valid
  • B3 = Invalid
  • B4 = Invalid

How it works

Explanation: Three tests are put together in AND: the start, the length, and whether the last 4 characters can be turned into a number.
Syntax: =IF(AND(LEFT(A2,2)="HR",LEN(A2)=7,ISNUMBER(--RIGHT(A2,4))),"Valid","Invalid")
HR-2041 passes all three: Valid. HR-20A1 ends with A1, which is not a number: Invalid. FIN-205 does not start with HR: Invalid.

Try it yourself in the live sheet

Type your formula in the highlighted cell and get instant feedback. Free, no Excel install needed.

Try this question in the live sheet →

AND syntax

=AND(logical1, [logical2], ...)

Returns TRUE only if every condition given is true. AND is used to build decision rules directly into a spreadsheet instead of manually judging each row. Full AND guide.

More AND practice questions

All practice questions · Excel exercises · Interview prep · Cheat sheet