Check string in cell

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
537
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Is there a formula I can use to check a cell for a specific format?

For example, I want to check that the first three characters are letters, followed by four numbers. So the first example below would be a positive match and the second would not. Similar to using "format" in VBA.
ABC1234
ABCD123
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this

Excel 2010
AB
1ABC1234TRUE
2ABCD1234FALSE
31234ABCDFALSE

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
B1=AND(IF(ISNUMBER(VALUE(LEFT(A1,3)))=TRUE,0,1),ISNUMBER(VALUE(MID(A1,4,4))))
B2=AND(IF(ISNUMBER(VALUE(LEFT(A2,3)))=TRUE,0,1),ISNUMBER(VALUE(MID(A2,4,4))))
B3=AND(IF(ISNUMBER(VALUE(LEFT(A3,3)))=TRUE,0,1),ISNUMBER(VALUE(MID(A3,4,4))))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
shyy, I think your solution may fail where there is something like this for example:

AB12345

KGee, another alternative to try might be something like this:

Code:
=AND(
    ISNA(
      LOOKUP(
        9.99999999999999E+307,
        -MID(A1, ROW(INDEX(A:A, 1):INDEX(A:A, 3)), 1))),
    ISNUMBER(-MID(A1, 4, 4)))
This checks for the first three characters not being numbers, and the next 4 characters being numbers.
You could add an additional constraint to the AND function to confirm the length of the string is 7 characters, but I'm not sure if you wanted to do that.
 
Upvote 0
shyy, I think your solution may fail where there is something like this for example:

AB12345

KGee, another alternative to try might be something like this:

Code:
=AND(
    ISNA(
      LOOKUP(
        9.99999999999999E+307,
        -MID(A1, ROW(INDEX(A:A, 1):INDEX(A:A, 3)), 1))),
    ISNUMBER(-MID(A1, 4, 4)))
This checks for the first three characters not being numbers, and the next 4 characters being numbers.
You could add an additional constraint to the AND function to confirm the length of the string is 7 characters, but I'm not sure if you wanted to do that.

Hi circledchicken

You formula checks if the last 4 characters are a number, but not if they are digits, which I think is what the OP needs.

For ex.

ABC1.23 passes the test.

also the first 3 characters should be letters.
 
Upvote 0
Hi

Try also:

=AND(IF({1,2,3,4,5,6,7}<=3,(MID(A1,{1,2,3},1)>="A")*(MID(A1,{1,2,3},1)<="Z"),ISNUMBER(-MID(A1,{1,2,3,4,5,6,7},1))))

You may add a check if the cell has only 7 characters, if needed.
 
Upvote 0
What does the {1,2,3,4,5,6,7} do?

Hi

Try also:

=AND(IF({1,2,3,4,5,6,7}<=3,(MID(A1,{1,2,3},1)>="A")*(MID(A1,{1,2,3},1)<="Z"),ISNUMBER(-MID(A1,{1,2,3,4,5,6,7},1))))

You may add a check if the cell has only 7 characters, if needed.
 
Upvote 0
I see, so if the cell has ABCDEF1234 you would do {1,2,3,4,5,6,7,8,9,10}

Thanks
 
Upvote 0
Hi circledchicken

You formula checks if the last 4 characters are a number, but not if they are digits, which I think is what the OP needs.
That's a good point - thanks for the correction.
Also, I really like your formula approach - very nice!
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,306
Members
449,095
Latest member
Chestertim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top