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
 
Hi circledchicken, shyy

I'm glad you liked it.

This was a simple case. If it's a more complex case, I like to use an input mask instead.

For ex., let's say we have 10 characters, the first 3 characters, the 5th and the last must be letters, the rest must be digits.
In this case it's easier to use a mask, like "AAA0A0000A" where the "A" stands for a letter and the "0" for a digit.

The formula would be:

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

You can, of course, have more complex variants like other options besides letters and digits.
Using a mask adds structure to the solution and the formula is easy to adapt.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
That's very nice - thanks for sharing that!

I was a little confused at first about the >=A and <=Z, because I thought it might be in some way related to the associated numbers you get when you extract the numeric code of a character using the CODE function.

But now I see it's making use of Excel's default alphanumeric sort sequence (i think!).

Do you have a computer science background? You come up with some great solutions.
 
Upvote 0
I think that this small modification in the Pgc01's formulas work too:

Code:
Formula1:
=AND(MID(A1,{1,2,3},1)>="A",MID(A1,{1,2,3},1)<="Z",MID(A1,{4,5,6,7},1)>="0",MID(A1,{4,5,6,7},1)<="9")

Formula2:
=AND(MID(A12,{1,2,3,5,10},1)>="A",MID(A12,{1,2,3,5,10},1)<="Z",MID(A12,{4,6,7,8,9},1)>="0",MID(A12,{4,6,7,8,9},1)<="9")

Markmzz
 
Upvote 0
Sorry for not posting back but this did resolve my issue and I was able to modify the code and use somewhere else so it was a double win for me!

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,895
Messages
6,127,624
Members
449,390
Latest member
joan12

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