Removing Text From Left side of alphanumeric string, different lengths of text - excel

=IF(LEFT(A2,3)="ABC","DEF"),RIGHT(A2,LEN(A2)-3),IF(LEFT(A2,2)="GH"),RIGHT(A2,LEN(A2)-2)
As this does not work, below is what I am attempting to accomplish and an example of it.
Essentially I am trying to remove the first few letters off of a SKU #. However the amount of letters before the SKU can vary and there is letters at the end of the SKU which I do not want removed.
Example:
AB12345
CDE54321XY
Z123
With a result of:
12345
54321XY
123
My knowledge and use of VBA is almost non-existent so I have not tried to do anything with that as I believe the line I have above could work with minor tweaking though I may be wrong.

It's not as good as a regular expression, and since there are only 10 digits, it turns out not too long. The below is an array formula (entered with Ctrl+Shift+Enter):
=MID(A1,MIN(IFERROR(SEARCH({0,1,2,3,4,5,6,7,8,9},A1),9^99)),LEN(A1))
The formula will give an error (#VALUE!) if there are no numbers. It basically looks for the position of a digit in the string and finds the smallest one. For the first example, SEARCH gets the positions: #VALUE!, 3 (1 is at position 3), 4 (2 is at position 4), 5, 6, 7, #VALUE!, #VALUE!, #VALUE!, #VALUE!. The smallest being 3 (I'm using IFERROR and a big number --9^99-- so it ignores the #VALUE!), the MID will take all characters as from the 3rd character inclusive.

Related

IF/COUNT/FIND formula help required

I have a bit of a dilemma with the IF COUNT FIND functions in Excel. I am currently researching goal times in soccer matches and have various games with goal times shown as follows (as an example);
23;56;85 (this would be in cell C2 for purpose of this question) The semi colons cannot be changed, this is how they are shown in the data I have, over 50,000 rows of data.
If I apply the formula below into a cell it will give the value "1" as it recognises the number 56.
=IF(COUNT(FIND({56},C2))>0, 1, 0)
However the formula does not work when I ask it to search for any single digit number from 1 to 9, for example the formula below would return the value "1" also because it is picking up the number 5 from the times of 56 and 85.
=IF(COUNT(FIND({5},C2))>0, 1, 0)
How can I adjust to formula so it picks up single digit goal times only? Any helps appreciated!
You can try to match the surrounding semi-columns in the process:
=IF(COUNT(FIND({";56;"},";"&C2&";"))>0, 1, 0)
Notice that (in both formulas) we match something surrounded by ;, but we also surrounded the searched string with ; at each side to handle the edges positions.
Your formula can be simplified further:
=--ISNUMBER(FIND(";56;",";"&C2&";"))
But your initial formula is good if you want later to match one of many values, which you can put in the array.

Excel check and replace the digit after decimal

I have a figure in Excel. For example Cell A1: 241.86.
How do I check and change the last digit. For example, if it is greater or equal to 6 then replace it to 5, else remain the same. Eg: 241.86 to 241.85.
I have tried the following:
=IF(MID(A1,FIND(".",A1)+2,5)>=6,REPLACE(A3,3,2,A1-0.01),A1)
The above formula doesn't work, whenever its 241.86 or 241.84 the result will be 241.85 and 241.83. It simply minus 1 from the last digit. Any help will be appreciated.
You are trying to manipulate a number. Replace() is a text function and will not do what you describe.
If you only want to round down numbers where the second decimal is greater than 5, then try
=IF(ROUND(MOD(A1,0.1),2)>=0.06,FLOOR(A1,0.05),A1)

IF function to identify first character of cell

I'm trying to use the IF function in Excel so that if the first character of a cell is 9, then the value shown should be the eight rightmost characters of that cell, otherwise the value shown should be the four rightmost characters. This formula however does not work:
=IF(LEFT(A2,1)=9,RIGHT(A2,8),RIGHT(A2,4))
It keeps returning the rightmost four numbers even though the number in cell A2 starts with 9.
Could you please point out what I'm doing wrong here?
LEFTreturns text, so the comparison needs to also be against a string:
=IF(LEFT(A2,1)="9",RIGHT(A2,8),RIGHT(A2,4))
or you need to convert the result of LEFT to a number again:
=IF(NUMBERVALUE(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
try =IF(INT(LEFT(A2,1))=9,RIGHT(A2,8),RIGHT(A2,4))
A shorter version if the condition is used to set the number of characters:
=RIGHT(A2,4+4*(LEFT(A2)="9"))

excel functions find pattern of a string in a cell

I have personal ID's in reports I have to find in one cell. Too bad the string in the cell which hides this ID can be anything, the ID can be at the beginning, the end, anywhere, but it is there.
The only thing I know is the pattern "space,letter,letter,number,number,number,number,number,number,space". Jike DB544345
I was looking for the correct word for this "mask", but couldn't find an answer. Thank you for your help.
As the comments are numerous I have created a minimal example that might represent what the OP is dealing with:
A1: 123456789 DB544345 asdfg asdfghjk
A2: creating dummy data is a DB544345 pain
A3: DB5443456 and soething else
parsed a copy of that in ColumnB with Text To Columns (with space as the delimiter) then applied:
=IFERROR(IF(AND(LEN(B1)=8,CODE(LEFT(B1))>64,CODE(LEFT(B1))<91,CODE(MID(B1,2,1))>64,CODE(MID(B1,2,1))<91,ISNUMBER(RIGHT(B1,6)*1),RIGHT(B1,6)*1>99999),B1,""),"")
to K1, copied this across to P1 and then K1:P1 down.
A concise "built-in function only" solution to a problem such as this requires a bit of tinkering as many attempts will dead-end or need workarounds due to deficiencies and quirks in the built-in Excel formulas. I much prefer single cell formulas because they minimally affect the general spreadsheet structure. However, due to the limitations listed above, complex single cell solutions often come at the cost of being rather long and cumbersome (this answer is somehow still only two lines on my formula bar in Excel). I came back to your question and cobbled together a formula that can (as far as I have tested) extract the first occurrence of this pattern with a single cell formula. This is an array formula (Ctrl+Shift+Enter instead of Enter) that assumes your data is in A2. This rough formula returns the first 8 characters if no match is found and throws #REF if the string is shorter than 10 characters.
=MID(A2,MIN(IF(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9))),1)=" ",IF(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+9,1)=" ",IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+1,1))>64,IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+1,1))<91,IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+2,1))>64,IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+2,1))<91,IF(IFERROR(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+3,6)*1>99999,FALSE),ROW(INDIRECT("A1:A"&(LEN(A2)-9)))))))))))+1,8)
Let me try to break this down at least on a high level. We are splitting the main text into every possible ten character chunk so that we can test each one using the suggestion of #pnuts to verify the Unicode values of the first two characters and run an ISNUMBER check on the rest of the string. This first block recurs throughout my formula. It generates a list of numbers from 1 to n-9 where n is the length of our main text string.
ROW(INDIRECT("A1:A"&(LEN(A2)-9)))
Let's assume our string is 40 characters long and replace the above formula with {1...31}. Using this number sequence generation we can check if characters 1 to 31 are spaces:
IF(MID(A2,{1...31},1)=" "
Then we can check if characters 10 to 40 are spaces:
IF(MID(A2,{1...31}+9,1)=" "
Then we can check if characters 2 to 32 are capital letters:
IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+1,1))>64,
IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+1,1))<91
Then we can check if characters 3 to 33 are capital letters:
IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+2,1))>64,
IF(CODE(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+2,1))<91
Then we can check if the strings of characters 4 to 9, 5 to 10, ..., 33 to 38, 34 to 39 are six-digit numbers:
IF(IFERROR(MID(A2,ROW(INDIRECT("A1:A"&(LEN(A2)-9)))+3,6)*1>99999,FALSE)
If all conditions are TRUE, that 10 digit chunk test will return the index of its first character in the string via another instance of the original array {1...31}. Otherwise it returns nothing. We take the Min of all return indexes and then use the Mid function to grab the 8 digit string determined by the aforementioned minimum index:
=MID(A2,MIN(matching index list)+1,8)
I think this will work, if we assume that the SPACE at the beginning and end are merely to differentiate the ID from the rest of the string; hence would not be present if the ID were at the beginning or end of the string. This formula is case insensitive. If case sensitivity is required, we could do character code comparisons.
=LOOKUP(2,1/((LEFT(myArr,2)>="AA")*(LEFT(myArr,2)<="ZZ")*(LEN(myArr)=8)*ISNUMBER(-RIGHT(myArr,6))),myArr)
Where myArr refers to:
=TRIM(MID(SUBSTITUTE(TRIM(Sheet2!A1)," ",REPT(" ",99)),(ROW(INDIRECT("1:10"))-1)*99+1,99))
If myArr is initially defined with the cursor in B1, referring to A1 as shown, it will adjust to refer to the cell one column to the left of the cell in which the Name appears.
The 10 in 1:10 is the maximum number of words in the string -- can be adjusted if required.

Comparing how many letters match

I'm trying to compare two cells in Excel to retrieve a numeric value, for example:
Trial type Duration Letters shown Distracters Laterality Response Full
6 80 XZHTEJ 0 Bilateral TEHZ
In that line I would like to compare the letters shown with the response, and return a number for how many are correct. I would like it to have the number 4, as there are four letters typed that match the letters shown.
VBA might be more suitable but you have not tagged as such and a formula should work:
=IFERROR(FIND(MID(C1,1,1),F1)>0,0)+IFERROR(FIND(MID(C1,2,1),F1)>0,0)+IFERROR(FIND(MID(C1,3,1),F1)>0,0)+IFERROR(FIND(MID(C1,4,1),F1)>0,0)++IFERROR(FIND(MID(C1,5,1),F1)>0,0)+IFERROR(FIND(MID(C1,6,1),F1)>0,0)
#pnuts' answer works if Letters Shown (in C1) is always 6 or fewer characters.
The following will work if Response (in F1) is always 4 or fewer characters:
=LEN(C1)-
LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(
C1,MID(F1,1,1),""),MID(F1,2,1),""),MID(F1,3,1),""),MID(F1,4,1),"")
)
Either method can easily be expanded as needed.
Just FYI, here is a solution for any length of response. It also has the advantage of being shorter than the other given solutions (I think)
{=SUM(IF(ISERROR(MATCH(MID(F1,ROW(INDIRECT("B1:B"&LEN(F1))),1),MID(C1,ROW(INDIRECT("A1:A"&LEN(C1))),1),0)),0,1))}
It's an array formula so leave out the curly braces and enter the formula using Ctrl+Shift+Enter
The solution creates two arrays like below
Shown Inputted
X T
Z E
H H
T Z
E
J
And then loops through each of the Inputted characters looking for a match in the Shown array and returning a 1 if there is a match, otherwise a 0. Finally the SUM adds all of the 1's (matches) to give the required result.

Resources