Splitting text string using formula...

Dan1000

Board Regular
Joined
Nov 4, 2006
Messages
210
I need to use a formula to split a text string using the commas as dividers with no spaces at either end of each string.

Maximum 4 commas to each string.

Is there a straight forward way of doing this rather than using the data to ccolumns excel command?

Regards
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I think you can do it VBA...

something along the lines of:

v = Split(cellref.Value, ",")

Then plant V into wherever you want to see the split:

For X = v(1)
For D = v(2)
For A = v(3)
For J = v(4)

These values refer to the position of the value in the now broken string....

I have seen it used on the openArgs of a userform in Access, not sure about excel though...
 
Upvote 0
Goto Visual Basic Editor and insert a module and paste the function.
Code:
Public Function SplitMyString(ByVal Rng As Range, ByVal iPos As Integer, ByVal sDelimiter As String) As String
Dim vSplit As Variant
Application.Volatile
vSplit = Split(Rng.Value, sDelimiter)
SplitMyString = vSplit(iPos - 1)
End Function
Then in the cell where you need specified data type the formula as:
=SplitMyString(A1,1,",")
This will give you first part (before 1st ",")
 
Upvote 0
Using native excel fomula

=TRIM(MID(SUBSTITUTE($A1,",",REPT(" ",LEN($A1))),1+(COLUMNS($B1:B1)*LEN($A1))-LEN($A1),LEN($A1)))

Where A1 holds the string, B1 holds the formula, then drag and fill right as needed.
 
Upvote 0
I need to use a formula to split a text string using the commas as dividers with no spaces at either end of each string.

Maximum 4 commas to each string.

Is there a straight forward way of doing this rather than using the data to ccolumns excel command?

Regards
Here's another one...

A2 = some string

Entered in B2 and copied across until you get blanks:

=TRIM(MID(SUBSTITUTE(","&$A2&REPT(",",6),",",REPT(" ",255)),COLUMNS($B2:B2)*255,255))
 
Upvote 0
Goto Visual Basic Editor and insert a module and paste the function.
Code:
Public Function SplitMyString(ByVal Rng As Range, ByVal iPos As Integer, ByVal sDelimiter As String) As String
Dim vSplit As Variant
Application.Volatile
vSplit = Split(Rng.Value, sDelimiter)
SplitMyString = vSplit(iPos - 1)
End Function
Then in the cell where you need specified data type the formula as:
=SplitMyString(A1,1,",")
This will give you first part (before 1st ",")

Hi Shrivallabha

Remark:
Application.Volatile makes the function inefficient and should only be used if absolutely necessary which is not the case.
 
Upvote 0
Here is a generic formula for obtaining the Nth field in a delimited text string...

=IF($A1="","",IF(fieldnumber=1,LEFT($A1,FIND(delimiter,$A1)-1),TRIM(MID(SUBSTITUTE($A1,delimiter,REPT(" ",LEN($A1))),LEN($A1)*(fieldnumber-1),LEN($A1)))))

Simply replace the two occurrences of the word "delimiter" with the text that is delimiting the fields (a comma for your current question) and replace the two occurrences of the word "fieldnumber" with the number of the field you want (1 is the field before the first comma, 2 is the field before the second comma, etc.). So, if A1 held the text you wanted to parse and B1 contained the field number you wanted, then this is the formula you would use for you comma delimited question...

=IF($A1="","",IF(B1=1,LEFT($A1,FIND(",",$A1)-1),TRIM(MID(SUBSTITUTE($A1,",",REPT(" ",LEN($A1))),LEN($A1)*(B1-1),LEN($A1)))))
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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