how to break a string in to substring in excel? - excel

original column is like:
0.45::rafas::4.0::0.0::0.9
0.35::rasaf::4.0::110.0::1.0
and i would like to break the string in to the following (:: as separator) in Excel
col1 col2 col3 col4 col5
0.45::rafas::4.0::0.0::0.9 0.45 rafas 4.0 0.0 0.9
0.35::rasaf::4.0::110.0::1.0 0.35 rasaf 4.0 110 1.0
Please help.

This page explains how to do just that using the "Text to Columns" function.
Copied for your convenience:
Highlight all of your cells with the data.
Select The Topmost Cell In The Column, E.G. A1 Hold CTRL+SHIFT And
Then Press The Down Arrow. OK, Once We've Done That, Go To "Data" Menu
And Select "Text To Columns". On The Text To Columns Window, Select
"Delimited" And Then Hit "Next". In The Following Window, Choose
"Other" For Type Of Delimiter And Use The Minus/Hyphen Sign - Hit
Finish.
Now you will have two columns, from your example, the first column
will contain data like "Animals" and the other column will contain the
data " House of The Rising Sun". (note the SPACE in front of "House")
To get rid of that SPACE we're going to use the TRIM function.
In cell C1 (or the column to the right of the song titles) type in
this formula.
=TRIM(B1)
Then double-click on that little black box on the excel cursor to copy
the formula down the whole range. Any spaces at the Start or end of
the text string will be removed.

If you wanted to do it with forumlae rather than the "text to columns" functions you could use:
Assuming string in A1
in B1: =FIND("::",$A1)
in C1: =FIND("::",$A1,B1+1)
Then copy C1 over D1:E1
in F1: =MID($A1,1,B1-1)
in G1: =MID($A1,B1+2,C1-B1-2)
Then copy G1 over H1:I1
And finally
in J1: =MID($A1,E1+2,LEN($A1)-E1-1)
The results of the split will be in F1:J1. You can always hide columns B:E as they are just internal to the splitting. This can then be done on as many rows as you need and if the value in A1 is update all other values will be changed. However, it is on a fixed number of columns but can easily be expanded if needed.

Excel (and OpenOffice) have a functionality to split Text into Columns. Highlight all the columns that conform to this schema, then go to the Data menu, and select "Text to Columns". Used a delimited separator and specify it as ":" while treating consecutive delimitors as one.

Here is a very simple way to extract the 5th character from the left from a text string in Excel:
Suppose the character string ABCDEFGHIJ is stored in cell A1 in an Excel Spreadsheet, then the following formula
=RIGHT(LEFT(A1,5),1)
produces the 5th character from the left in the string, namely “E”.

If you would like a simple function, you can use the following VBA code.
Function SplitTextToNum(rngInput As Range, sepString As String)
Dim CallerRows As Long, CallerCols As Long, DimLimit As Long
Dim outvar As Variant
outvar = Split(rngInput.Value, sepString, -1, vbBinaryCompare)
If Application.Caller.Rows.Count > 1 Then
SplitTextToNum = Application.Transpose(outvar)
Else
SplitTextToNum = outvar
End If
End Function
You can use Ctrl+Shift+Enter over a range of cells after entering the formula referring to the cell in which you have the string that you need to be split up.

Related

Convert value of one cell into many rows, same ID value

I have one cell like this:
930 "<h2>Crawford, 1963-</h2> [wpse_playlist type=""audio"" tracklist=""true"" tracknumbers=""true"" images=""true"" artist=""true""] [wpse_trac title=""Indian Summer"" src=""http://urlnamehere/bitstream/handle/2374.UC/731507 /Elliston_Marisa_Crawford_01-10-14_Track_01.mp3?sequence=7"" type=""audio/mpeg"" caption="""" description="""" meta_artist=""Crawford, Marisa & Pendarvis, Jack, 1963-"" meta_length_formatted="""" thumb_src="""" image_width=""300""][wpse_trac title=""The River"" src=""http://urlnamehere/bitstream/handle/2374.UC/731507/Elliston_Marisa_Crawford_01-10-14_Track_02.mp3?sequence=8"" type=""audio/mpeg"" caption="""" description="""" meta_artist=""Crawford, Marisa & Pendarvis, Jack, 1963-"" meta_length_formatted="""" thumb_src="""" image_width=""300""][wpse_trac title=""Swimming in Lanes Five and Six
I'd like to break it up so that I get one row for each title value and src value, while keeping the same id (930 in this case. Thus:
Row 1:
930 | wpse_trac title=""Indian Summer" | src=""http://urlnamehere/bitstream/handle/2374.UC/731507 /Elliston_Marisa_Crawford_01-10-14_Track_01.mp3?sequence=7"
Row 2
930 | [wpse_trac title=""The River"" src=""http://urlnamehere/bitstream/handle/2374.UC/731507/Elliston_Marisa_Crawford_01-10-14_Track_02.mp3?sequence=8
Place the text in cell A1.
Go to Data tab and select Text to Columns
Choose Delimited and press Next.
Check Other: and key in ] and press Next.
In Destination, select a cell other than A1 and click Finish.
You will see your original text separated into many columns. At this point follow the following steps:
Press Ctrl+H in your keyboard to open the replace text dialog.
In Find what:, enter " type* and click Replace All.
Select all the cells that have the resulting data, copy them and
paste them as Transpose to paste them in rows instead of columns.
Remove the first and last cells (which are not desired)
Complete the desired task with some tiny touches made by you. ENJOY
Try the following formula if your text is in cell A1 and drag the formula down:
=MID($A$1,1,SEARCH(" ",$A$1)-1)&" | "&MID($A$1,SEARCH("£wpse_trac title",SUBSTITUTE($A$1,"wpse_trac title","£wpse_trac title",ROW(A1))),SEARCH("£http:",SUBSTITUTE($A$1,"http:","£http:",ROW(A1)))-SEARCH("£wpse_trac title",SUBSTITUTE($A$1,"wpse_trac title","£wpse_trac title",ROW(A1)))-8)&" | "&MID($A$1,SEARCH("£http:",SUBSTITUTE($A$1,"http:","£http:",ROW(A1)))-6,SEARCH(" ",$A$1,SEARCH("£sequence",SUBSTITUTE($A$1,"sequence","£sequence",ROW(A1))))+10-SEARCH("£http:",SUBSTITUTE($A$1,"http:","£http:",ROW(A1)))-6)

how to separate text with multiple underscores and varying length of values

a1=ac_tree_birch_NewYork_ext
a2=bc_animal_dog_Washington_des
How do I separate the text in the cells by the "_", since there is varying length of the cell values. I would like to use a formula, and not text to columns.
Thanks
Use the SUBSTITUTE function to change all underscores (e.g. CHAR(95)) to a large number of spaces (typically the entire length of the original string) and peel out the padded pieces with the MID function. Finish off with TRIM and an IFERROR 'wrapper'.
In B1 as,
=IFERROR(TRIM(MID(SUBSTITUTE($A1, CHAR(95), REPT(CHAR(32), LEN($A1))), (COLUMN(A:A)-1)*LEN($A1)+1, LEN($A1))), TEXT(,))
Fill both right and down.
  
This can likely be done via Flash Fill (Excel 2013+).
For the first row of data, enter your expected outcome in subsequent cells to the right. This is how you want the data broken up:
Then select your first cell of output data and click Flash Fill from the ribbon:
Do this for the remaining columns. This will fill the column based on the pattern recognized by Excel within your original data:
If a VBA solution is acceptable, you can write a wrapper around the VBA Split function:
Public Function Split2(s As String) As String()
Split2 = Split(s, "_")
End Function
Then in your worksheet, select (say) cells B1:F1, enter
=Split2(A1)
as an array function (CTRL-SHIFT-ENTER), and out comes your data.
Hope that helps.

How to merge rows in a column into one cell in excel?

E.g
A1:I
A2:am
A3:a
A4:boy
I want to merge them all to a single cell "Iamaboy"
This example shows 4 cells merge into 1 cell however I have many cells (more than 100), I can't type them one by one using A1 & A2 & A3 & A4 what can I do?
If you prefer to do this without VBA, you can try the following:
Have your data in cells A1:A999 (or such)
Set cell B1 to "=A1"
Set cell B2 to "=B1&A2"
Copy cell B2 all the way down to B999 (e.g. by copying B2, selecting cells B3:B99 and pasting)
Cell B999 will now contain the concatenated text string you are looking for.
I present to you my ConcatenateRange VBA function (thanks Jean for the naming advice!) . It will take a range of cells (any dimension, any direction, etc.) and merge them together into a single string. As an optional third parameter, you can add a seperator (like a space, or commas sererated).
In this case, you'd write this to use it:
=ConcatenateRange(A1:A4)
Function ConcatenateRange(ByVal cell_range As range, _
Optional ByVal separator As String) As String
Dim newString As String
Dim cell As Variant
For Each cell in cell_range
If Len(cell) <> 0 Then
newString = newString & (separator & cell)
End if
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(separator)))
End If
ConcatenateRange = newString
End Function
Inside CONCATENATE you can use TRANSPOSE if you expand it (F9) then remove the surrounding {}brackets like this recommends
=CONCATENATE(TRANSPOSE(B2:B19))
Becomes
=CONCATENATE("Oh ","combining ", "a " ...)
You may need to add your own separator on the end, say create a column C and transpose that column.
=B1&" "
=B2&" "
=B3&" "
In simple cases you can use next method which doesn`t require you to create a function or to copy code to several cells:
In any cell write next code
=Transpose(A1:A9)
Where A1:A9 are cells you would like to merge.
Without leaving the cell press F9
After that, the cell will contain the string:
={A1,A2,A3,A4,A5,A6,A7,A8,A9}
Source: http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/
Update: One part can be ambiguous. Without leaving the cell means having your cell in editor mode. Alternatevly you can press F9 while are in cell editor panel (normaly it can be found above the spreadsheet)
Use VBA's already existing Join function. VBA functions aren't exposed in Excel, so I wrap Join in a user-defined function that exposes its functionality. The simplest form is:
Function JoinXL(arr As Variant, Optional delimiter As String = " ")
'arr must be a one-dimensional array.
JoinXL = Join(arr, delimiter)
End Function
Example usage:
=JoinXL(TRANSPOSE(A1:A4)," ")
entered as an array formula (using Ctrl-Shift-Enter).
Now, JoinXL accepts only one-dimensional arrays as input. In Excel, ranges return two-dimensional arrays. In the above example, TRANSPOSE converts the 4×1 two-dimensional array into a 4-element one-dimensional array (this is the documented behaviour of TRANSPOSE when it is fed with a single-column two-dimensional array).
For a horizontal range, you would have to do a double TRANSPOSE:
=JoinXL(TRANSPOSE(TRANSPOSE(A1:D1)))
The inner TRANSPOSE converts the 1×4 two-dimensional array into a 4×1 two-dimensional array, which the outer TRANSPOSE then converts into the expected 4-element one-dimensional array.
This usage of TRANSPOSE is a well-known way of converting 2D arrays into 1D arrays in Excel, but it looks terrible. A more elegant solution would be to hide this away in the JoinXL VBA function.
For those who have Excel 2016 (and I suppose next versions), there is now directly the CONCAT function, which will replace the CONCATENATE function.
So the correct way to do it in Excel 2016 is :
=CONCAT(A1:A4)
which will produce :
Iamaboy
For users of olders versions of Excel, the other answers are relevant.
For Excel 2011 on Mac it's different. I did it as a three step process.
Create a column of values in column A.
In column B, to the right of the first cell, create a rule that uses the concatenate function on the column value and ",". For example, assuming A1 is the first row, the formula for B1 is =B1. For the next row to row N, the formula is =Concatenate(",",A2). You end up with:
QA
,Sekuli
,Testing
,Applitools
,Visual Testing
,Test Automation
,Selenium
In column C create a formula that concatenates all previous values. Because it is additive you will get all at the end. The formula for cell C1 is =B1. For all other rows to N, the formula is =Concatenate(C1,B2). And you get:
QA,Sekuli
QA,Sekuli,Testing
QA,Sekuli,Testing,Applitools
QA,Sekuli,Testing,Applitools,Visual Testing
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation
QA,Sekuli,Testing,Applitools,Visual Testing,Test Automation,Selenium
The last cell of the list will be what you want. This is compatible with Excel on Windows or Mac.
I use the CONCATENATE method to take the values of a column and wrap quotes around them with columns in between in order to quickly populate the WHERE IN () clause of a SQL statement.
I always just type =CONCATENATE("'",B2,"'",",") and then select that and drag it down, which creates =CONCATENATE("'",B3,"'",","), =CONCATENATE("'",B4,"'",","), etc. then highlight that whole column, copy paste to a plain text editor and paste back if needed, thus stripping the row separation. It works, but again, just as a one time deal, this is not a good solution for someone who needs this all the time.
I know this is really a really old question, but I was trying to do the same thing and I stumbled upon a new formula in excel called "TEXTJOIN".
For the question, the following formula solves the problem
=TEXTJOIN("",TRUE,(a1:a4))
The signature of "TEXTJOIN" is explained as TEXTJOIN(delimiter,ignore_empty,text1,[text2],[text3],...)
I needed a general purpose Concatenate With Separator (since I don't have TEXTJOIN) so I wrote this:
Public Function ConcatWS(separator As String, ParamArray cell_range()) As String
'---concatenate with seperator
For n = LBound(cell_range) To UBound(cell_range)
For Each cell In cell_range(n)
If Len(cell) <> 0 Then
ConcatWS = ConcatWS & IIf(ConcatWS <> "", separator, "") & cell
End If
Next
Next n
End Function
Which allows us to go crazy with flexibility in including cell ranges:
=ConcatWS(" ", Fields, E1:G2, L6:M9, O6)
NOTE: "Fields" is a Named Range and the separator may be blank

Append same text to every cell in a column in Excel

How can I append text to every cell in a column in Excel? I need to add a comma (",") to the end.
Example:
email#address.com turns into email#address.com,
Data Sample:
m2engineers#yahoo.co.in
satishmm_2sptc#yahoo.co.in
threed_precisions#rediffmail.com
workplace_solution#yahoo.co.in
threebworkplace#dataone.in
dtechbng#yahoo.co.in
innovations#yahoo.co.in
sagar#mmm.com
bpsiva#mmm.com
nsrinivasrao#mmm.com
pdilip#mmm.com
vvijaykrishnan#mmm.com
mrdevaraj#mmm.com
b3minvestorhelpdesk#mmm.com
sbshridhar#mmm.com
balaji#mmm.com
schakravarthi#mmm.com
srahul1#mmm.com
khramesh2#mmm.com
avinayak#mmm.com
rockindia#hotmail.com
See if this works for you.
All your data is in column A (beginning at row 1).
In column B, row 1, enter =A1&","
This will make cell B1 equal A1 with a comma appended.
Now select cell B1 and drag from the bottom right of cell down through all your rows (this copies the formula and uses the corresponding column A value.)
Select the newly appended data, copy it and paste it where you need using Paste -> By Value
That's It!
It's a simple "&" function.
=cell&"yourtexthere"
Example - your cell says Mickey, and you want Mickey Mouse. Mickey is in A2. In B2, type
=A2&" Mouse"
Then, copy and "paste special" for values.
B2 now reads "Mickey Mouse"
It's simple...
=CONCATENATE(A1, ",")
Example: if email#address.com is in the A1 cell then write in another cell: =CONCATENATE(A1, ",")
email#address.com After this formula you will get email#address.com,
For remove formula: copy that cell and use Alt + E + S + V or paste special value.
There is no need to use extra columns or VBA if you only want to add the character for display purposes.
As this post suggests, all you need to do is:
Select the cell(s) you would like to apply the formatting to
Click on the Home tab
Click on Number
Select Custom
In the Type text box, enter your desired formatting by placing the number zero inside whatever characters you want.
Example of such text for formatting:
If you want the cell holding value 120.00 to read $120K, type $0K
Pretty simple...you could put all of them in a cell using the concatenate function:
=CONCATENATE(A1, ", ", A2, ", ", and so on)
Highlight the column and then Ctrl + F.
Find and replace
Find ".com"
Replace ".com, "
And then one for .in
Find and replace
Find ".in"
Replace ".in, "
Simplest of them all is to use the "Flash Fill" option under the "Data" tab.
Keep the original input column on the left (say column A) and just add a blank column on the right of it (say column B, this new column will be treated as output).
Just fill in a couple of cells of Column B with actual expected output. In this case:
m2engineers#yahoo.co.in,
satishmm_2sptc#yahoo.co.in,
Then select the column range where you want the output along with the first couple of cells you filled manually ... then do the magic...click on "Flash Fill".
It basically understands the output pattern corresponding to the input and fills the empty cells.
I just wrote this for another answer:
You would call it using the form using your example: appendTextToRange "[theRange]", ",".
Sub testit()
appendTextToRange "A1:D4000", "hey there"
End Sub
Sub appendTextToRange(rngAddress As String, append As String)
Dim arr() As Variant, c As Variant
arr = Range(rngAddress).Formula
For x = LBound(arr, 1) To UBound(arr, 1)
For y = LBound(arr, 2) To UBound(arr, 2)
Debug.Print arr(x, y)
If arr(x, y) = "" Then
arr(x, y) = append
ElseIf Left(arr(x, y), 1) = "=" Then
arr(x, y) = arr(x, y) & " & "" " & append & """"
Else
arr(x, y) = arr(x, y) & " " & append
End If
Next
Next
Range(rngAddress).Formula = arr
End Sub
Select the range of cells, type in the value and press Ctrl + Enter.
This, of course, is true if you want to do it manually.
Put the text/value in the first cell, then copy the cell, mark the whole colum and 'paste' the copied text/value.
This works in Excel 97 - sorry no other version available on my side...
This is addition to #Edward-Leno 's answer for more detail/explanation and cases where the text cells are formulas instead of values, and you want to retain the original formula.
Suppose your cells look like this (formulas)
="email" & "#" & "address.com"
=A1 & "#" & C1
instead of this (values)
email#address.com
If "email" and "address.com" were some cells like A1 is the email and C1 is the address.com part, then you'd have something like =A1&"#"&C1 which would be important to retain since A1 and C1 might not be constants and can change, so the comma-concatenated values would change, like if C1 is "gmail.com", "yahoo.com", or something else based on its formula.
Values method: The following steps will successfully append text but only keep the value using a scratch column (this works for rows, too, but for simplicity, the directions are for columns)
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=A1&","
Essentially, the "&" is the concatenation operator, combining two strings together (numbers are converted to strings). The "," can be adjusted to ", " if you want a space after the comma.
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Formula retention method: The following steps will successfully retain the original formula. The process is similar to the values method, and only step 2, the formula used to concatenate the comma, changes.
Assume column A is your data.
In scratch column B, start anywhere like the top of column B such as at B1 and put this formula:
=FORMULATEXT(A1)&","
FORMULATEXT() grabs the formula of the cell as opposed to the value of it, so a simple example would be that it grabs =2+2 instead of 4, or =A1 & "#" & C1 where A1 is "Bob" and C1 is "gmail.com" instead of Bob#gmail.com.
Note: This formula only works for Excel versions 2013 and greater. For alternative equivalent solutions for Excel 2010 and older, see this superuser answer: https://superuser.com/a/894441/495155
Copy the cell B1 and copy it down to all other cells in column B, either by clicking at the bottom right of cell B1 and dragging down, or copying with "Ctrl+C" or right-click > "Copy".
Paste B1 to all cells in column B with "Ctrl+V" or right-click > "Paste Options:" > "Paste". You should see the data looking like you intended.
Copy all cells in column B and paste them to where you want via right-click > "Paste Options:" > "Values". We select values so it doesn't mess up any formatting or conditional formatting
Type it in one cell, copy that cell, select all the cells you want to fill, and paste.
Alternatively, type it in one cell, select the black square in the bottom-right of that cell, and drag down.

Add common prefix to all cells in Excel

I have a column with some text in each cell.
I want to add some text, for example "X", at the start of all cells. For example:
A B
----- >>>> ----
1 X1
2 X2
3 X3
What is the easiest way to do this?
Type this in cell B1, and copy down...
="X"&A1
This would also work:
=CONCATENATE("X",A1)
And here's one of many ways to do this in VBA (Disclaimer: I don't code in VBA very often!):
Sub AddX()
Dim i As Long
With ActiveSheet
For i = 1 To .Range("A65536").End(xlUp).Row Step 1
.Cells(i, 2).Value = "X" & Trim(Str(.Cells(i, 1).Value))
Next i
End With
End Sub
Select the cell you want to be like this,
Go To Cell Properties (or CTRL 1)
under Number tab
in custom
enter
"X"#
Select the cell you want to be like this, go to cell properties (or CTRL 1) under Number tab in custom enter "X"#
Put a space between " and # if needed
Select the cell you want,
Go To Format Cells (or CTRL+1),
Select the "custom" Tab, enter your required format like : "X"#
use a space if needed.
for example, I needed to insert the word "Hours" beside my numbers and used this format : # "hours"
Enter the function of = CONCATENATE("X",A1) in one cell other than A say D
Click the Cell D1, and drag the fill handle across the range that you want to fill.All the cells should have been added the specific prefix text.
You can see the changes made to the repective cells.
Option 1:
select the cell(s), under formatting/number/custom formatting, type in
"BOB" General
now you have a prefix "BOB" next to numbers, dates, booleans, but not next to TEXTs
Option2:
As before, but use the following format
_ "BOB" #_
now you have a prefix BOB, this works even if the cell contained text
Cheers, Sudhi
Michael.. if its just for formatting then you can format the cell to append any value.
Just right click and select Format Cell on the context menu, select custom and then specify type as you wish... for above example it would be X0. Here 'X' is the prefix and 0 is the numeric after.
Hope this helps..
Cheers...
Go to Format Cells - Custom. Type the required format into the list first. To prefix "0" before the text characters in an Excel column, use the Format 0####. Remember, use the character "#" equal to the maximum number of digits in a cell of that column. For e.g., if there are 4 cells in a column with the entries - 123, 333, 5665, 7 - use the formula 0####. Reason - A single # refers to reference of just one digit.
Another way to do this:
Put your prefix in one column say column A in excel
Put the values to which you want to add prefix in another column say column B in excel
In Column C, use this formula;
"C1=A1&B1"
Copy all the values in column C and paste it again in the same selection but as values only.
Type a value in one cell (EX:B4 CELL). For temporary use this formula in other cell (once done delete it). =CONCAT(XY,B4) . click and drag till the value you need. Copy the whole column and right click paste only values (second option).
I tried and it's working as expected.

Resources