You can achieve this using a combination of data validation and a formula. Here's a step-by-step guide to set this up:
- Select cell H8.
- Go to the "Data" tab on the ribbon, and click "Data Validation" > "Data Validation."
- In the "Data Validation" dialog box, choose "Custom" in the "Allow" dropdown list.
- In the "Formula" field, enter the following formula:
=AND(LEN(H8)=19, ISNUMBER(--MID(H8,11,2)), ISNUMBER(--MID(H8,14,4)), CODE(MID(LOWER(H8),1,1))>=97, CODE(MID(LOWER(H8),1,1))<=122, CODE(MID(LOWER(H8),3,1))>=97, CODE(MID(LOWER(H8),3,1))<=122, CODE(MID(LOWER(H8),4,1))>=97, CODE(MID(LOWER(H8),4,1))<=122, CODE(MID(LOWER(H8),8,1))>=97, CODE(MID(LOWER(H8),8,1))<=122, CODE(MID(LOWER(H8),9,1))>=97, CODE(MID(LOWER(H8),9,1))<=122, MID(H8,2,1)="-", MID(H8,5,1)="-", MID(H8,10,1)="-", MID(H8,13,1)="-")
This formula checks if the input in H8 is in the required format a-aaa-aaaa-aa-00-0000. It ensures that the dashes are in the correct positions, letters are in the specified positions, and numbers are in the specified positions.
- Click "OK" to apply the data validation to cell H8.
Now, users will only be able to enter data in the required format in cell H8. If they enter data in an incorrect format, Excel will show a data validation error message.
To automatically convert the entered text to lowercase and add dashes if they are not entered by the user, you can use a Worksheet Change event in VBA:
- Right-click the sheet tab where cell H8 is located, and choose "View Code."
- In the Visual Basic for Applications (VBA) editor, paste the following code:
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$H$8" Then Application.EnableEvents = False Target.Value = LCase(Target.Value) Target.Value = Application.WorksheetFunction.Substitute(Target.Value, "-", "") Target.Value = Mid(Target.Value, 1, 1) & "-" & Mid(Target.Value, 2, 3) & "-" & Mid(Target.Value, 5, 4) & "-" & Mid(Target.Value, 9, 2) & "-" & Mid(Target.Value, 11, 2) & "-" & Mid(Target.Value, 13, 4) Application.EnableEvents = True End If End Sub
- Close the VBA editor.
Now, when users enter data in cell H8, the text will automatically be converted to lowercase, and the dashes will be added in the correct positions if they were not entered by the user. Note that this VBA code assumes that the user has entered the correct number of characters (14 characters in total: 10 letters and 4 numbers). The data validation will ensure that the input is in the correct format.
Excel_learnez, thanks for taking the time to reply. Aside from having the same issue as JDV, which is that the formula is too long for a custom format validation, I think your formula was also off in a couple of places. However, it did give me some food for thought, and I have fixed it up so that it now validates the format properly. At least now I have a functional formula, even if I can't enter it into the custom format validation. I'll probably enter the formula into a hidden cell on the worksheet, and then use a value=TRUE formula in the custom validation. For those who are interested, the corrected formula I finally got to work was:
=AND(LEN(H8)=21,ISNUMBER(--MID(H8,15,2)),ISNUMBER(--MID(H8,18,4)),CODE(MID(LOWER(H8),1,1))>=97,CODE(MID(LOWER(H8),1,1))<=122,CODE(MID(LOWER(H8),3,1))>=97,CODE(MID(LOWER(H8),3,1))<=122,CODE(MID(LOWER(H8),4,1))>=97,CODE(MID(LOWER(H8),4,1))<=122,CODE(MID(LOWER(H8),5,1))>=97,CODE(MID(LOWER(H8),5,1))<=122,CODE(MID(LOWER(H8),7,1))>=97,CODE(MID(LOWER(H8),7,1))<=122,CODE(MID(LOWER(H8),8,1))>=97,CODE(MID(LOWER(H8),8,1))<=122,CODE(MID(LOWER(H8),9,1))>=97,CODE(MID(LOWER(H8),9,1))<=122,CODE(MID(LOWER(H8),10,1))>=97,CODE(MID(LOWER(H8),10,1))<=122,CODE(MID(LOWER(H8),12,1))>=97,CODE(MID(LOWER(H8),12,1))<=122,CODE(MID(LOWER(H8),13,1))>=97,CODE(MID(LOWER(H8),13,1))<=122,MID(H8,2,1)="-",MID(H8,6,1)="-",MID(H8,11,1)="-",MID(H8,14,1)="-",MID(H8,17,1)="-")
With regard to your VBA code for inserting the "-"s, that worked fine. The only thing I didn't really like about it is that it does mean the cell is pre-populated with "-----" as the default. If there is a way to hide this, that would be good. However, it's not a biggie.