texto to column by formula

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
337
Office Version
  1. 365
Platform
  1. Windows
Hi,

trying to trim this to the second delimiter by using a formula

111-11-1
result
111-11

exp2
ddd-dd-1
result
ddd-dd
 
When I enter this formula in a macro it says "invalid character" refering to "|"

=LEFT(A1,SEARCH("|",SUBSTITUTE(A1,"-","|",2))-1)

This worked for me

Code:
Range("B1").Formula = "=LEFT(A1,SEARCH(""|"",SUBSTITUTE(A1,""-"",""|"",2))-1)"

Observe the double double-quotes in the formula string.

M.
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I m trying to place
=LEFT(A1,SEARCH("|",SUBSTITUTE(A1,"-","|",2))-1)
in place of the other formula that's there but can't make it work

Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)).FormulaR1C1 = "=IFERROR(LEFT(RC[-1],FIND(""-"",RC[-1],FIND(""-"",RC[-1])+1)-1),"""")"
 
Upvote 0
This worked for me

Code:
Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _
    FormulaR1C1 = "=LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1)"

M.
 
Upvote 0
The | shouldn't be a problem, but the worksheet functions don't always exist in VBA without a .application() or evaluate(). Find and replace are available in both. Show me the whole line and I'll fix it.
 
Last edited:
Upvote 0
oops... i missed the IFERROR

Try
Code:
Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _
    FormulaR1C1 = "=IFERROR(LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1),"""")"

M.
 
Upvote 0
oops... i missed the IFERROR Try
Code:
 Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _     FormulaR1C1 = "=IFERROR(LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1),"""")"
M.
I get a runtime error 1004
 
Upvote 0
The | shouldn't be a problem, but the worksheet functions don't always exist in VBA without a .application() or evaluate(). Find and replace are available in both. Show me the whole line and I'll fix it.
this is the whole line, it's giving me a runtime error 1004 Sub othertrim() ' ' othertrim Macro ' Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _ FormulaR1C1 = "=IFERROR(LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1),"""")" ' End Sub
 
Upvote 0
Marcelo's solution in #15 worked when I tried it:


Excel 2010
A
1
223223-43232365-222-1
32334-2211-2-2-23456-86765-2235
41-1-1-234565
51-9-3456-76543-1111-1257788-54332221
Sheet3


click on B1 (remember, you have an activecell property in the code), then run:

Code:
Sub text2columnformulaincells()
Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _
    FormulaR1C1 = "=IFERROR(LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1),"""")"
End Sub

and you get:


Excel 2010
AB
223223-43232365-222-123223-43232365
32334-2211-2-2-23456-86765-22352334-2211
41-1-1-2345651-1
51-9-3456-76543-1111-1257788-543322211-9
Sheet3
Cell Formulas
RangeFormula
B2=IFERROR(LEFT(A2,SEARCH("|",SUBSTITUTE(A2,"-","|",2))-1),"")
B3=IFERROR(LEFT(A3,SEARCH("|",SUBSTITUTE(A3,"-","|",2))-1),"")
B4=IFERROR(LEFT(A4,SEARCH("|",SUBSTITUTE(A4,"-","|",2))-1),"")
B5=IFERROR(LEFT(A5,SEARCH("|",SUBSTITUTE(A5,"-","|",2))-1),"")
 
Upvote 0
This worked for me
Code:
 Range(Cells(2, ActiveCell.Column), Cells(Cells(Rows.Count, ActiveCell.Column - 1).End(xlUp).Row, ActiveCell.Column)). _     FormulaR1C1 = "=LEFT(RC[-1],SEARCH(""|"",SUBSTITUTE(RC[-1],""-"",""|"",2))-1)"
M.
Thank you Marcelo
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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