Excel; how to combine left and right functions - excel

I have a column of text values that I want to delete the first 8 and last 4 characters from i.e '12345678john1234' would just become 'john'. I know how to use the left and right functions separately but can't seem to get them working together. The mid function won't work in this case. Is this possible? Thanks

Dear Use this formula and you can get your answer
=LEFT(RIGHT(A1,8),4)

Problem solved with:
=MID(a1,9,LEN(a1)-12)
Explaination:
MID: get the middle string
=MID(text,start_nums,numchars)
start_nums = 9 to start from the character j
numchars = len(text)-12 = 16 - 12 = 4
Then you will get the result as john

Related

Excel - Deleting zeros on the right side of the cells

I would like to delete the zeros on the right side of the cells if there are more then 3 zeros.
Example:
A B
12345 12345
1230 1230
12345600 12345600
12000 12000
12340000000000000 1234000
1234500000000000000000 12345000
Is it possible to excel using just formula in the cells of the column B??
How to do?
Thanks so much!
The answers, given until now, are treating the numbers as strings, while I'd go for the numeric approach:
if mod(number,10000) = 0
then number = number div 1000;
return number;
Which means: if the number, divided by 10,000 equals 0 (if the number ends with '0000') then return the number, divided by a thousand (remove the last three zeroes).
You don't need this one time, but you need to remove all triplets of three zeroes, as much as possible, so instead of a simple if-loop, you might go for a while-loop:
while mod(number,10000) = 0
do number = number div 1000;
return number;
You can use this in a VBA function:
Public Function remove_ending_blanks(r As Range) As Double
Dim temp As Double
temp = r.Value
While temp Mod 10000 = 0
temp = temp / 1000
Wend
remove_ending_blanks = temp
End Function
You might also do this, using a formula, but the while-loop will need to be done using a circular reference, which is quite tricky.
Try this shorter formula solution and worked in left max. 3 zeros on the right side.
In B1, formula copied down :
=0+TRIM(LEFT(A1,MATCH(9^9,INDEX(1/MID(A1,ROW($1:$99),1),0))+3))
Bit of a stretch (I'm prety sure it can be done better), but if you have access to TEXTJOIN, try the following in B2:
=IF(RIGHT(A1,4)="0000",FILTERXML("<t><s>"&TEXTJOIN("</s><s>",1,MID(A1,1,LEN(A1)-ROW(A$1:INDEX(A:A,LEN(A1)))))&"</s></t>","//s[substring(., string-length(.)-3) != 0]"),A1)
Or:
=IF(RIGHT(A7,4)="0000",LEFT(A7,MAX((MID(A7,ROW(A$1:INDEX(A:A,LEN(A7))),1)<>"0")*(ROW(A$1:INDEX(A:A,LEN(A7)))))+3),A7)
Note: It's an array formula and needs to be confirmed through CtrlShiftEnter
It looks frightening, agreed, but would yield the correct result as far as my testing went. For example 100000400000 would yield 1000004000.
you could use this formula
=IF(RIGHT(A1,4)="0000",LEFT(A1,LEN(A1/10^LEN(A1))-FIND(".",A1/10^LEN(A1))+3),A1)
Consider:
Public Function ZeroTrimmer(r As Range) As String
s = r.Text
While Right(s, 4) = "0000"
s = Left(s, Len(s) - 1)
Wend
ZeroTrimmer = s
End Function
Edit: These will give incorrect results. I mis-read the question and didn't realize you wanted to leave a max of 3 trailing zeroes. The below will remove all trailing zeroes.
If it's an unknown number of trailing zeroes, it gets tricky. You'd have to use something like this...
=(10^(LEN(RIGHT(VALUE(CONCATENATE("0.", A2)),LEN(VALUE(CONCATENATE("0.", A2)))-FIND(".",VALUE(CONCATENATE("0.", A2)))))))*VALUE(CONCATENATE("0.", A2))
If you know the number of trailing zeroes it becomes much easier, and can be done like this:
=LEFT(A2,LEN(A2)-3)
Where 3 in the above formula represents the number of trailing zeroes to remove. Another variation could be:
=A2/(10^3)
These formulas will work if text or numeric.

Removing and Adding Characters to a cell

I was wondering if it is possible to remove the first 2 numbers from 8200001 and then chance it to 90 instead in ONE formula?
Example
8200001 = 9000001
5822581 = 9022581
9688888 = 9088888
The REPLACE function is perfect here:
=REPLACE(A1,1,2,90)
1 is where to start from (the position in your string)
2 is the number of digits to replace
90 is what to replace them with
This returns a string, so if you want a number add -- before the function:
=--REPLACE(A1,1,2,90)
Yes, use MID to parse:
=--(90&MID(A1,3,LEN(A1)))

Adding 0's In the middle of a cell?

I know how to add leading 0's, but I was wondering if it is posible to add them in a middle of a cell to = 12 characters. The first two characthers are always going to be letters. and then nummbers.
Example
AB45686 = AB0000045686
DS456 = DS0000000456
Parse the string with LEFT and MID, then use text to add the needed 0s:
=LEFT(A1,2) & TEXT(--MID(A1,3,LEN(A1)),"0000000000")
Here is the other way to get the result.

SSIS: Delete everything to left of a character in a string

Note: this is SSIS not sql server
I am pulling data from a file and some columns have names like this:
1;&count chocula
13;&roger ramjet
123;&mary smith
45678;&john adams
How do I remove the ampersand and everything to the left of it?
I am using the fx transformation for the character.
I thought about finding the character position for the ampersand and then deleting everthing from start to that position but ssis does not have that function. The ampersand can be at any position, I cannot say it is guaranteed to be in position such and such.
Thanks
The RIGHT() function retrieves the last X characters of a string.
RIGHT("13;&roger ramjet",12) = roger ramjet
Above, X equals 12. Of course, twelve won't work for every string. Instead we can calculate X by subtracting the string length from the position of the ampersand.
LEN(MyColumn]) = 16
FINDSTRING([MyColumn],"&",1) = 4
Or put another way...
RIGHT([MyColumn], LEN([MyColumn]) - FINDSTRING([MyColumn],"&",1)) = roger ramjet

Return all text to the right of last numeric digit in strings

Return the right text string of a cell until the first number from the right e.g.
"geb. 14 oct 1956 Westerkerk HRL" must return Westerkerk HRL
"geb. 14 oct 1956" must return empty cell
" " must return empty cell
Formula solution:
=TRIM(MID(A1,1+MAX(FIND({1,2,3,4,5,6,7,8,9,0},A1&"1234567890")*ISNUMBER(FIND({1,2,3,4,5,6,7,8,9,0},A1))),LEN(A1)))
EDIT: I realized that the above formula may run into issues if there is a repeated number (for example if it had been 1966 instead of 1956), so I went back and came up with this which will not be affected by that issue:
=TRIM(MID(A1,1+LOOKUP(2,1/ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),ROW(INDIRECT("1:"&LEN(A1)))),LEN(A1)))
However, INDIRECT is a volatile function and I usually try to avoid it. You could instead use a number which is going to be higher than the length of your text strings in order to accomplish the same goal without a volatile function, though this may not actually be any faster than using the INDIRECT function in the formula:
=TRIM(MID(A1,1+LOOKUP(2,1/ISNUMBER(--MID(A1,ROW($1:$255),1)),ROW($1:$255)),LEN(A1)))
So basically, you can use either formula, whichever you prefer.
A bit of monkey work - someone might have a simpler solution - but first you'd need to get the highest lastIndexOf '0' through '9'. In your example, the number is '6'. From that, you can create a substring.
var c = 'geb. 14 oct 1956 Westerkerk HRL'; // c for companyname
var subc = '';
var startsub = c.lastIndexOf('6') + 2; // Point past the space to the W.
if(c.length > startsub) subc = c.substring(startsub, c.length);
return subc;

Resources