Controlling a cells format - Alpha Numeric (x4) Alpha Alpha

Dobbsylondon

New Member
Joined
Oct 21, 2014
Messages
6
Good day all,

First post, never lurked.

I'm confident you guys are the ones to ask though.
In an existing sheet, I'd like to control the format of cells in a column. Namely restrict it to ANNNNAA (A=Text and N=numbers), 7 characters maximum as it's really meant to accept only existing unique ID numbers.

I've tried to use \####\\ or "",####"","" which may or may not obviously work as a custom. As my understanding is at it's limit, I thought it time to ask for some help.


Thank you all in advance and I hope to repay the favour at some point.

Kind regards
 
Give this formula a try...

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

Note1: The formula is a modification of one 'pgc01' posted here... http://www.mrexcel.com/forum/excel-questions/719296-check-string-cell.html#post3543996

Note2: The letters in the cell are assumed to always be entered in upper case.
There is a minor flaw with the above formula... it returns TRUE for entries that are longer than 7 characters so long as the first 7 characters meet the stated condition. This revision fixes that problem...

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

As for a custom formula for use in Data Validation, this version of the formula seems to work...

=IF(LEN(A1)=7,AND(IF(MID("A0000AA",ROW(1:7),1)="A",(MID(A1,ROW(1:7),1)>="A")*(MID(A1,ROW(1:7),1)<="Z"),ISNUMBER(-MID(A1,ROW(1:7),1)))))
 
Last edited:
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Your formula will accept non-alphanumeric characters in the first, sixth and seventh positions; for example, it will return TRUE for "/1234-/" (without the quotes, of course).

Thanks, I see now. Then try this with 6 functions (MID*MID part was borrowed from post #11):

=SUM((MID(A1,{1,6,7},1)>="A")*(MID(A1,{1,6,7},1)<="Z"),ISNUMBER(1*MID(A1,{2,3,4,5},1))*{10,10,10,10})+100*(LEN(A1)=7)=143
 
Upvote 0
Well,
I thought this should solve it.

=AND(LEN(A1)=7,CellControlMatchRegEx("[A-Za-z]{1}[0-9]{4}[A-Za-z]{2}",A1))

The code for the custom function

Code:
Function CellControlMatchRegEx(StrPattern As String, CellRng As Range) As Boolean

    Dim regEx As Object
    Set regEx = CreateObject("vbscript.regexp")

    regEx.Pattern = StrPattern
    regEx.IgnoreCase = True
    regEx.Global = True
    
    Dim IsSame As Object
    Set IsSame = regEx.Execute(CellRng.Value)

    If IsSame.Count > 0 Then CellControlMatchRegEx = True Else CellControlMatchRegEx = False

End Function

Unfortunately it turned out that excel doe not allow some things to be used in data validation amng them Custom functions (it accepts them as named ranges).

The workaround this i could think of is to use some of the suggested in the thread formulas (the one you like best) in another cell e.g. B1 - it will evaluate to eithe TRUE or FALSE.

Then for custom data validation on A1 you could use:

=B1=TRUE

guaranteed to work.
Hope this helps.
 
Upvote 0
Thanks, I see now. Then try this with 6 functions (MID*MID part was borrowed from post #11):

=SUM((MID(A1,{1,6,7},1)>="A")*(MID(A1,{1,6,7},1)<="Z"),ISNUMBER(1*MID(A1,{2,3,4,5},1))*{10,10,10,10})+100*(LEN(A1)=7)=143
This looks good... I can find no incorrect evaluations in my quick testing of it. Nice going, although I think we can simplify your formula a little bit by getting rid of the math an using a straight AND function call...

=AND((MID(A1,{1,6,7},1)>="A"),(MID(A1,{1,6,7},1)<="Z"),ISNUMBER(1*MID(A1,{2,3,4,5},1)),LEN(A1)=7)
 
Last edited:
Upvote 0
This looks good... I can find no incorrect evaluations in my quick testing of it. Nice going, although I think we can simplify your formula a little bit by getting rid of the math an using a straight AND function call...

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

Wrapping in AND is a good idea. Thanks for streamlining the formula.
 
Upvote 0
Holy moley.

You guys are awesome. Thanks very much for this, you guys are life savers AND I may well have learned a little in the process.

Kindest regards


EDIT-


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

I'm guessing this goes into the VBA editor. But i'm at a loss as to where it's inserted.
 
Last edited:
Upvote 0
I'm guessing this goes into the VBA editor. But i'm at a loss as to where it's inserted.

No, it doesn't go in VBE - this is the formula to input in a cell in the worksheet.
The only VBA code in this thread is in (my) post #13.

But as I see it - all you need eventually is a combination of:
- the quoted formula (from post #14)
- and read the blue text in my post #13.
- no VBA - all done within the worksheet
 
Upvote 0
No, it doesn't go in VBE - this is the formula to input in a cell in the worksheet.
The only VBA code in this thread is in (my) post #13.

But as I see it - all you need eventually is a combination of:
- the quoted formula (from post #14)
- and read the blue text in my post #13.
- no VBA - all done within the worksheet

If the OP is really looking for a Data Validation formula, this one (which I posted in Message #11) seems to work correctly without needing any helper cells...

=IF(LEN(A1)=7,AND(IF(MID("A0000AA",ROW(1:7),1)="A",(MID(A1,ROW(1:7),1)>="A")*(MID(A1,ROW(1:7),1)<="Z"),ISNUMBER(-MID(A1,ROW(1:7),1)))))
 
Upvote 0
Holy moley.

You guys are awesome. Thanks very much for this, you guys are life savers AND I may well have learned a little in the process.

Kindest regards


EDIT-




I'm guessing this goes into the VBA editor. But i'm at a loss as to where it's inserted.

Not in the VBA editor; you should put the formula in the spreadsheet, in a column next to your data column. For example, if your data is in column A, starting from A1, then put the formula in B1 and copy down. Copy down means: after confirming the formula in B1 with Enter, place the mouse over the bottom right corner of cell B1 and when the fill handle (a plus symbol) appears, left click on it and drag it down as long as there is data in the data column. Alternatively, if there is not a blank cell in A, you can copy down the formula by clicking twice on the fill handle.
Excel Workbook
AB
1B0000GTTRUE
2A00z0AfFALSE
3/1200A+FALSE
40000AfFALSE
5A4567AfTRUE
6A4567Af3FALSE
7AT234AfFALSE
Sheet
 
Upvote 0
If the OP is really looking for a Data Validation formula, this one (which I posted in Message #11) seems to work correctly without needing any helper cells...

=IF(LEN(A1)=7,AND(IF(MID("A0000AA",ROW(1:7),1)="A",(MID(A1,ROW(1:7),1)>="A")*(MID(A1,ROW(1:7),1)<="Z"),ISNUMBER(-MID(A1,ROW(1:7),1)))))


Would I change A1 to (in the case of my sheet) B:B to apply it to the whole B column?

Many thanks to all involved, and I really do appreciate every bit of advice.. Just to clarify. The sheet is already up and working on a networked drive and I just want to regulate - "guide" people who are forgetful in entering case numbers in the correct format. So am a bit hesitant to modify the structure by adding columns and changing existing layouts in case I nerf something already in place. Data validation has always seemed the least obtrusive way to me.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,628
Members
449,241
Latest member
NoniJ

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