Trim everything before a character

urobee

Board Regular
Joined
Jan 30, 2015
Messages
98
Hy,

I have a text looks like:

"abcdef | 123456 | xyz"

I need to show only the last few character after the last " | " character, (delete everything before the last " | " and the pipe character too)
so in this case i need: "xyz" (the number of the characters is different all the time)

Oh, and i need to do this with VBA, not with formula.

Thanks for your help! :)
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
If the text is cell A2, the below code will return the desired result.

Code:
MsgBox Evaluate("=TRIM(RIGHT(SUBSTITUTE(A2,""|"",REPT("" "",99)),99))")
 
Upvote 0
If the text is cell A2, the below code will return the desired result.

Code:
MsgBox Evaluate("=TRIM(RIGHT(SUBSTITUTE(A2,""|"",REPT("" "",99)),99))")

Thanks, it works fine on a sheet if i use it like a formula without the "msgBox" part.
But i want to use it on an userform textbox to show the result. When i tried to use the code i got this error message: "Argument not optional".
The original text i want to trim is located in a combobox, so the code looks like this:
Code:
Trim(Right(Substitute(comboL.Value, "|", Rept(" ", 99)), 99))
I deleted some " character because i got "Invalid character" error when i use four " character.
 
Upvote 0
urobee,

If A1 contains abcdef | 123456 | xyz

Then in B1 try =TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)))


Oh, and i need to do this with VBA, not with formula.

Sorry, I missed that part.
 
Last edited:
Upvote 0
Thanks, it works fine on a sheet if i use it like a formula without the "msgBox" part.
But i want to use it on an userform textbox to show the result. When i tried to use the code i got this error message: "Argument not optional".
The original text i want to trim is located in a combobox, so the code looks like this:
Code:
Trim(Right(Substitute(comboL.Value, "|", Rept(" ", 99)), 99))
I deleted some " character because i got "Invalid character" error when i use four " character.

Substitute and Rept are not VBA functions, rather, they are Excel worksheet functions, so you need to use the Evaluate function in order for your VBA code to use them. However, here is a VBA only code line that will return the value you want...
Code:
[table="width: 500"]
[tr]
	[td]MsgBox Trim(Mid(ComboL.Value, InStrRev(ComboL.Value, "|") + 1))[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
urobee,

If A1 contains abcdef | 123456 | xyz

Then in B1 try =TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)))




Sorry, I missed that part.


I tried to modify your original code and it works right now :)
Here is the modified code:
Code:
Trim(Right(WorksheetFunction.Substitute([COLOR=#333333]comboL.Value[/COLOR], "|", WorksheetFunction.Rept(" ", 99)), 99))

Thanks for your help! :) :)
 
Upvote 0
Substitute and Rept are not VBA functions, rather, they are Excel worksheet functions, so you need to use the Evaluate function in order for your VBA code to use them. However, here is a VBA only code line that will return the value you want...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]MsgBox Trim(Mid(ComboL.Value, InStrRev(ComboL.Value, "|") + 1))[/TD]
[/TR]
</tbody>[/TABLE]


wow, what a good solution, thanks you too! :)
 
Upvote 0
Another alternative is;

Code:
MsgBox Split(ComboL.Value, "|")(2)
Your alternative will only work if there are always exactly three delimited fields, otherwise using a hard-coded 2 would not work (the OP's use of the phrase "everything before the last ' | '" seems to suggest otherwise). Also, you would want to Trim the array element otherwise you would have a leading space whenever there was a space following the pipe symbol. The more general way to use Split for this would be...

TempArr = Split(ComboL.Value, "|")
MsgBox Trim(TempArr(UBound(TempArr)))
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,665
Members
449,462
Latest member
Chislobog

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