Need help stripping different length texts into two parts

zrall

New Member
Joined
Dec 19, 2012
Messages
6
As an example, I have the following sets of accounts. Under Account is what I have, and Number and Type are what I need returned. As a disclaimer, none of these are real accounts, but the lengths are all representative.

[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD]Account
[/TD]
[TD]Number
[/TD]
[TD]Type
[/TD]
[/TR]
[TR]
[TD]*ZHR|U1234567
[/TD]
[TD]U1234567
[/TD]
[TD]*ZHR
[/TD]
[/TR]
[TR]
[TD]STID|U1234567 1
[/TD]
[TD]U1234567
[/TD]
[TD]STID
[/TD]
[/TR]
[TR]
[TD]IR|Q45678989
[/TD]
[TD]Q4567898
[/TD]
[TD]IR
[/TD]
[/TR]
[TR]
[TD]DQW|ITERW|Q4765567 1
[/TD]
[TD]Q4765567
[/TD]
[TD]DQW|ITERW
[/TD]
[/TR]
[TR]
[TD]CVR|L1425690
[/TD]
[TD]L1425690
[/TD]
[TD]CVR
[/TD]
[/TR]
</TBODY>[/TABLE]

All of the Number columns need to be the first letter followed by 7 digits, although some of the Accounts have 8-9 characters after the letter. The Type column needs to strip off the final "|" and anything following that (again tricky because of differing character lengths). If it matters, there are a total of 48 different Type categoris and 4 different possible numbers before the 7 characters in the Number categories. Also, the number combinations are seemingly infinite.

I'm pretty certain this requires VBA to perform. I've been messing around with it all day but haven't been able to get anywhere. I would greatly appreciate some help with this.

Thanks.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Put these formulas where indicated and then copy them down...

B2: =TRIM(RIGHT(SUBSTITUTE(LEFT(A2,FIND(" ",A2&" ")-1),"|",REPT(" ",99)),99))

C2: =LEFT(A2,FIND(B2,A2)-2)
 
Upvote 0
Hi Rick,

Can you please explain this part "|",REPT(" ",99)),99) in the formula?

I have seen this being used many times in text extraction formulas. But cant figure out exact thing it does.

Thanks in advance!!

Jai
 
Upvote 0
Put these formulas where indicated and then copy them down...

B2: =TRIM(RIGHT(SUBSTITUTE(LEFT(A2,FIND(" ",A2&" ")-1),"|",REPT(" ",99)),99))

C2: =LEFT(A2,FIND(B2,A2)-2)

That workd nearly perfectly. The only problem is that some of the initial cells have 9 total characters (see: Q45678989) while others have 8 total characters (see: U1234567). I need the Number column to only return 8 total characters, cutting Q45678989 down to Q4567898 and keeping U1234567 as U1234567.
 
Upvote 0
Hi Rick,

Can you please explain this part "|",REPT(" ",99)),99) in the formula?

I have seen this being used many times in text extraction formulas. But cant figure out exact thing it does.

Thanks in advance!!

Jai
Consider this example (and note instead of using 99, I used 9 to make the text small enough to print, but the reasoning as to why it works is the same)...

=TRIM(RIGHT(SUBSTITUTE("one|two|three","|",REPT(" ",9)),9))

The REPT function produces a text string of 9 spaces and the SUBSTITUTE function substitutes them for each pipe symbol (|) in the text. This results in...
Code:
"one         two         three"
(without the quotes, of course) where there are 9 spaces between the words. The key to the number (9 here, 99 in my original formula) is that it must be equal to or greater than the number of characters in all the text following the last pipe symbol (based on your examples, I judged 99 would do that). Okay, now take the right-most 9 characters (again, this number must be equal to or greater than the number of character following the last pipe symbol). This yields...
Code:
"    three"
(again, without the quotes) four spaces followed by the ending text... the TRIM function handles removing those leading space characters to leave you with just the text that followed the last pipe symbol.
 
Last edited:
Upvote 0
That workd nearly perfectly. The only problem is that some of the initial cells have 9 total characters (see: Q45678989) while others have 8 total characters (see: U1234567). I need the Number column to only return 8 total characters, cutting Q45678989 down to Q4567898 and keeping U1234567 as U1234567.
Just wrap the formula with a LEFT function call specifying the number of characters you want...

B2: =LEFT(TRIM(RIGHT(SUBSTITUTE(LEFT(A2,FIND(" ",A2&" ")-1),"|",REPT(" ",99)),99)),8)
 
Upvote 0
That makes sense. I was still trying to decipher all of the steps in the formula. Everything works perfectly now. Much appreciated and you are truly great at excel!
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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