Add leading 0 after the dash (-) but before the numbers - excel

I have a formula that adds a 0 before the numbers 1-9 if they don't have one.
Old..................New
D-8..................D-08
FE-09..............FE-09
I-18..................I-18
P-1..................P-01
FG-08A...........FG-08
=LEFT(A1,FIND("-",A1))&TEXT(MID(A1,FIND("-",A1)+1,2),"00")
However, for values like FG-08A, I do not want it to clear the A at the end. So if it's FG-08A, the result would be FG-08A (remain unchanged). If it's FG-8A, it would be FG-08A (adds the leading 0 but keeps the "A").

Add check for the ending letter:
=LEFT(A1,FIND("-",A1))&TEXT(MID(A1,FIND("-",A1)+1,ISNUMBER(--MID(A1,FIND("-",A1)+1,2))+1),"00")&IF(ISERROR(--RIGHT(A1)),RIGHT(A1),"")

As stated in my comment, if you never have more than two consecutive numbers and if these will never be 00, you can use:
=SUBSTITUTE(SUBSTITUTE(A1,"-","-0"),"00","0")

Related

Formatting number with a bunch of zeroes

Somewhat simple problem:
I need to turn a column A, which contains numbers with up to 1 decimal (20, 142, 2.5, etc.) to a string with a specific format, namely 8 whole digits and 6 decimal digits but without the actual decimal period, like so:
1 = 00000001000000
13 = 00000013000000
125 = 00000125000000
46.5 = 00000046500000
For what it's worth, the input data from column A will never be more than 3 total digits (0.5 to 999) and the decimal will always be either none or .5.
I also need for Excel to leave the zeroes alone instead of auto-formatting as a number and removing the ones at the beginning of the string.
As a makeshift solution, I've been using =CONCATENATE("'",TEXT(A1,"00000000.000000")), then copying the returning value and "pasting as value" where I actually need it.
It works fine, but I was wondering if there was a more direct solution where I don't have to manually intervene.
Thanks in advance!
=TEXT(A1*1000000,"0000000000000") I think that's what you mean.

Alpha Numeric Validation in Microsoft Excel

1- I'd like to use a validation rule for an input cell where the entry must be 7 or 8 alphanumeric characters long
2- at the start of the string Alphas used must be 1 or 2 characters and uppercase.
3- at the end of the string Numerics will always be 6 characters long.
4- The following type of entries are required to be validated
FD456789
X256325
Z899666
DQ985421
FD000052
5-I have created a validation formula. it works fine except it cannot validate 2nd character as alphabate in the string. i used AP656569 and A5656569 for testing. it should allow only AP656569, but on the contrary it is allowing both strings.
Formula: =AND(OR(LEN(A3)=7,LEN(A3)=8),ISNUMBER(VALUE(RIGHT(A3,6))),IF(LEN(A3)=7,NOT(ISNUMBER(VALUE(LEFT(A3,1)))),ISTEXT(MID(A3,2,1))))
You may try:
=AND(AND(LEN(A1)>6,LEN(A1)<9,ISNUMBER(RIGHT(A1,6)*1),CODE(A1)>64,CODE(A1)<91),IF(LEN(A1)=8,AND(CODE(MID(A1,2,1))>64,CODE(MID(A1,2,1))<91),1))
=AND( - Let's check two things:
AND( - Check if multiple conditions are TRUE:
LEN(A1)>6 - Check if string is over 6 char.
LEN(A1)<9 - Check if string in under 9 chars.
ISNUMBER(RIGHT(A1,6)*1 - Check if 6 rightmost characters make up a numeric value.
CODE(A1)>64,CODE(A1)<91 - Check if leftmost characters is in class [A-Z].
IF( - Check the following:
LEN(A1)=8 - Check if the lengths is actually 8.
AND( - If TRUE then check the following:
CODE(MID(A1,2,1))>64,CODE(MID(A1,2,1))<91 - Check if 2nd char is in class [A-Z].
1 - If the length is not false, it will still be 7, therefor we return a 1 (equal to TRUE), to not mess with our parent AND().
You can apply this to your custom validation rule as a formula if you want to avoid false data, or as mentioned in the comments to conditional formatting if you want to be able to show false data after it being entered.
Alternatively, if you have Excel 2019 or higher, and you like code-golf you could use:
=AND(ISNUMBER(RIGHT(A1,6)*1),CODE(A1)>64,CODE(A1)<91,SWITCH(LEN(A1),7,1,8,AND(CODE(MID(A1,2,1))>64,CODE(MID(A1,2,1))<91),0))
Your conditions do not exclude a string like A1234567 (1 capital letter, 7 digits). According to your conditions and assuming your string is in cell A1, this formula should work:
=AND(OR(LEN(A1)=7,LEN(A1)=8),OR(IFERROR(LEFT(A1,1)*1,0)=0,AND(IFERROR(LEFT(A1,1)*1,0)=0,IFERROR(LEFT(A1,2)*1,0)=0)),UNICODE(A1)=UNICODE(UPPER(A1)),UNICODE(MID(A1,2,1))=UNICODE(UPPER(MID(A1,2,1))),IFERROR(MID(RIGHT(A1,6),1,1)*1,0),IFERROR(MID(RIGHT(A1,6),2,1)*1,0),IFERROR(MID(RIGHT(A1,6),3,1)*1,0),IFERROR(MID(RIGHT(A1,6),4,1)*1,0),IFERROR(MID(RIGHT(A1,6),5,1)*1,0),IFERROR(MID(RIGHT(A1,6),6,1)*1,0))
It's basically an AND function that contains:
a condition to check for the lenght of the string: OR(LEN(A1)=7,LEN(A1)=8)
a condition to check if first 2 characters of the string are letters (only the first or both): OR(IFERROR(LEFT(A1,1)*1,0)=0,AND(IFERROR(LEFT(A1,1)*1,0)=0,IFERROR(LEFT(A1,2)*1,0)=0))
a condition to check if the first character is capital: UNICODE(A1)=UNICODE(UPPER(A1))
a condition to check if the second character is capital: UNICODE(MID(A1,2,1))=UNICODE(UPPER(MID(A1,2,1)))
a condition for each last 6 characters to check if they are numeric (example refers to the first one): IFERROR(MID(RIGHT(A1,6),1,1)*1,0)
EDIT: Improvements
The formula can be improved like this:
=AND(OR(LEN(A1)=7,LEN(A1)=8),OR(IFERROR(LEFT(A1,1)*1,0)=0,AND(IFERROR(LEFT(A1,1)*1,0)=0,IFERROR(LEFT(A1,2)*1,0)=0)),EXACT(LEFT(A1,2),UPPER(LEFT(A1,2))),ISNUMBER(RIGHT(A1,6)*1))
It's still an AND function. This the changes:
it contains a single condition to check if the first 2 characters are capital (previously there were 1 for each character that used the UNICODE function): EXACT(LEFT(A1,2),UPPER(LEFT(A1,2))) [CREDIT: JvdV]
it contains a single condition for the last 6 characters to check if they are numeric (previously there were 1 for each character that used the IFERROR function): ISNUMBER(RIGHT(A1,6)*1)
EDIT: correction
In order to exclude special character, i've edited the formula:
=AND(OR(LEN(A1)=7,LEN(A1)=8),OR(AND(UNICODE(A1)>64,UNICODE(A1)<91,ISNUMBER(MID(A1,2,1)*1)),AND(UNICODE(A1)>64,UNICODE(A1)<91,UNICODE(MID(A1,2,1))>64,UNICODE(MID(A1,2,1))<91)),EXACT(LEFT(A1,2),UPPER(LEFT(A1,2))),ISNUMBER(RIGHT(A1,6)*1))

Need to add 0 to match the length

I have unique identifiers for each row. For example 19Jan187938 or 19Jan206414 but there are some which are like 19Jan17333. I need to add a 0 before the number if it's 5 digits, so it becomes 19Jan017333.
I tried,
=TEXT(CONCATENATE(19,AB2,C2),"000000")
even with 11 0's, since the total length is 11. Nothing changes.
Try the following:
=CONCATENATE(LEFT(AB2,5),TEXT(RIGHT(AB2,LEN(AB2)-5),"000000"))
It will basically, take the first 5 characters and concatenate that with the remaining characters formatted as a six digit number with leading zeroes
If your identifier is on A1, you can try this:
=IF(LEN(A1)<11;CONCATENATE(LEFT(A1;5);RIGHT("000000"&MID(A1;6;5);6));A1)
See what happens.

How would I remove all leading alphabetical characters?

I am interested in removing leading alphabetical (alpha) characters from cells which appear in a column. I only wish to remove the leading alpha characters (including UPPER and LOWER case): if alpha characters appear after a number they should be kept. Some cells in the column might not have leading alpha characters.
Here is an example of what I have:
36173
PIL51014
4UNV22001
ZEB54010
BICMPAFG11BK
BICMPF11
Notice how there are not always the same number of leading alpha characters. I cannot simply use a Left or Right function in Excel, because the number of characters I wish to keep and remove varies.
A correct output for what I am looking for would look like:
36173
51014
4UNV22001
54010
11BK
11
Notice how the second to last row preserved the characters "BK", and the 3rd row preserved "UNV". I cannot simply remove all alpha characters.
I am a beginner with visual basic and was not able to figure out how to use excel functions to address my issue. How would I do this?
Here is an Excel formula that will "strip off the leading alpha characters" Actually, it looks for the first numeric character, and returns everything after that:
=MID(A1,MIN(FIND({0;1;2;3;4;5;6;7;8;9},A1&"0123456789")),99)
The 99 at the end needs to be some value longer than the longest string you might be processing. 99 usually works.
Here's a formula based solution complete with test results:
=MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"),255),100)
Change the 100 at the end if any string may be longer than 100 characters. Also the 255 is not needed, but it won't hurt.
This short UDF should strip off leading alphabetic characters.
Function noLeadAlpha(str As String)
If Not IsNumeric(str) Then
Do While Asc(str) < 48 Or Asc(str) > 57
str = Mid(str, 2)
If Not CBool(Len(str)) Then Exit Do
Loop
End If
noLeadAlpha = str
End Function
        
Koodos Jeeped, you beat me to it.
But here is an alternative anyway:
Function RemoveAlpha(aString As String) As String
For i = 1 To Len(aString)
Select Case Mid(aString, i, 1)
Case "0" To "9"
RemoveAlpha = Right(aString, Len(aString) - i + 1): Exit For
End Select
Next i
End Function

if third letter is equal to

I have a cell which contains any 4 letters, example akei, skiw. How do I ask
"if the 3rd letter is an i then equals True"
I was thinking something like
"=if(a1="??i?", True, False)"
But that dosen't Work
=MID(A1,3,1) = "i"
Should work, you don't need to use IF, the evaluation using the equals will return either TRUE or FALSE
The MID function let you to select a portion of the text, if you set the Position Start and the numbers of characters you want
=MID(A1,3,1) = "i"
so you just compare it to "i"
You could use the wildcard approach if you use COUNTIF like this
=COUNTIF(A1,"??i?")
That will return 1 or 0 and effectively tests two things, that A1 contains 4 characters AND the third one is "i"
As with MID this isn't case-sensitive so 1 will be returned for both XXIX and zziz
So I came upon this question for a similar use: Expanding a numbering system for lab specimen with each digit distinguishing something different and the 3rd digit indicating a heating profile
The resulting function was used to expand these codes out for people that would not know my logic otherwise:
=IF(MID(B2,3,1) = "1", "Temp1°C for 1 hour", "Temp2°C for X hours")

Resources