How to concatenate strings from hundreds of cells? - excel

I am trying to combine sentences from various cells into one cell. Example:
A1 - "hello"
A2 - "how are"
A3 - "you"
A4 - =combine(A1:A3) ---- > "hello how are you"
I know this trick: =A1 & " " & A2..... but I have 700 cells to combine into a single cell and this approach seems barbaric. If there is a built in function, that would be ideal. I don't mind an answer in VBA as long as the answer is extremely detailed starting with how to open VBA, as I do not know VBA.

There is but it isn't much better: concatenate.
In your case, that would be concatenate(A1;" ";A2;" ";A3).
Neither are good ways to deal with 700 cells.
A VBA solution would be better. To open the VBA editor, press ALT+F11 (for a graphical explanation, see this article).
Afterwards, go to the "Insert" menu on top and select "Module". The editor will then be ready to accept input. Just paste the following (delete any other text that it may have):
Option Explicit
Public Function MyConcatenate(ByVal myRange As Range)
Dim vCell, vResult As String
For Each vCell In myRange.Cells
vResult = vResult & " " & vCell.Text
Next
MyConcatenate = Mid(vResult, 2)
End Function
You can now close the editor and return to the workbook.
To use it, write the following in a cell: =MyConcatenate(A1:A3).

You can use the StringConcat function found in this page: http://www.cpearson.com/excel/stringconcatenation.aspx
Then you can use it like this:
=StringConcat("|",B1:B5)
An you will received all values from the range: B1:B5 separated by |.
In case the link is broken, you can find the source of the function here: http://pastebin.com/JNS9pYWA.

For a relatively modest 700 cells formulae would seem viable.
in B1: =A1&" "
in C1: =B1
in B2: copy B1 (ie =A2&" ")
in C2: =C1&B2
then copy down B2:C2 as required.

I too have the same problem. I have data in column A from A1:A980.
But I have found out the solution as below.
In B2 I put the formula as =CONCATENATE(B1," ",A1," ",A2)
and from B3 I entered formula as =CONCATENATE(B2," ",A3) and in B3 as =CONCATENATE(B3," ",A4) till B980.
This will give you your result in last B90 cell and that too without any VBA.
Hope you might have the same problem then this might solve the issue.

Related

How to use 3 cells to actually complete the test

Ok, so I did have a quick search but did not find an answer that does what I would like... I have a horrible feeling that it is simple.. :)
I would like a formula in cell D1 that will use cells A1, B1 and C1 to evaluate the test shown in B1. This is for producing questions.
The picture shows the data and results.
I have tried indirect() with no success and &, ie
=A1&B1&C1
You can use the Evaluate method in excel VBA. Note that i'm using .Value on the rng2. This is because we want to use the displayed value of the cell (in the format in which it appears to the user lookins at the cell) rather than the underlying value that excel stores in the cell (which is what .value2 gives us).
Public Function EEE(rng As Range, rng2 As Range, rng3 As Range)
EEE = Evaluate(rng.Value2 & rng2.Value & rng3.Value2)
End Function
Note that there is a method of doing it using only worksheet functions, but it rather complicated compared to the VBA solution. An excellent descriptio of the old Evaluate() function and how to use it is given in this article.
Following formula could be used to evaluate the result of combination of A1, B1, C1
=ISLOGICAL(A1&B1&C1)
or
=ISLOGICAL(CONCATENATE(A1,B1,C1))

excel function to combine cells into single line of text?

I'm new to stack overflow so I apologize if this is a horrendously stupid question. I am wondering if there is a function or way to code a function in excel that will combine a column of cells with plain text and convert them into one cell with the text on a single line? Specifically I want to convert a column of random numbers into a single line of text and insert SPACE+AND+SPACE between them.
Ex.
15133484
12345188
12345888
to
15133484 AND 12345188 AND 12345888
Currently I am copying and pasting all this information into google and then into Word and using find/replace and it is taking forever everytime. If it is possible to just get Excel to do this for me that would be amazing.
Thanks!
If you have Office 365 Excel use TEXTJOIN():
=TEXTJOIN(" AND ",TRUE,A:A)
otherwise one would have to use:
=A1 & " AND " & A2 & " AND " & A3
Or one can use a helper column, B1 put:
=A1
put this in B2 and copy down:
=IF(A2<>"",B1 & " AND " & A2,B1)
And grab the last cell in column B.
A little late, but still:
Reference here
Step 1:
=concatenate(transpose(rngBeg:rngEnd & " AND "))
Step 2:
highlight the transpose statement and then press F9, which substitutes the actual values for the formula.
Step 3:
Remove the curly braces, { }, from the formula. The cell will display the range of reference cells combined with whatever separator chosen after the ampersand sign.
Not a "live" formula, but still far easier than manually concatenating a range of values.
Press ALT+F11 to open Microsoft Visual Basic for Applications,
Insert-> Module
Paste this:
Function Combine(WorkRng As Range, Optional Sign As String = " AND ") As String
Dim Rng As Range
Dim OutStr As String
For Each Rng In WorkRng
If Rng.Text <> "," Then
OutStr = OutStr & Rng.Text & Sign
End If
Next
Combine = Left(OutStr, Len(OutStr) - 5)
End Function
In any cell type =Combine(Range)
i.e.
=Combine(A1:A500)
use concat function if you can add an additional column in the excel like this:
=CONCAT(D3:E5)
Attached sample image with input, additional column, output and formula
I assume you want to merge the data in the 3 cells into a single cell with a space between the 3 data set.
If that is the case then you can do it simply by using the Concatenate function in excel.
In the above example, you have data in Cells A1, A2 & A3.
Cell C1 has the merged data. As you can see, we have used CONCATENATE Function.
The space has been defined in Double quotes. So if you need a Hyphen (-), you can put that in Double Quotes with space “ - ” and it will display the result with Sanjay - Singh - Question
Hope this helps.

Delete from a String: All Words that Appear in a Column in Excel

I've found workarounds for this problem - but I'm having a hard time imagining there isn't a simpler solution than what I have.
Let's Suppose in Column A, I have cells with the following words in them...
A1: Until
A2: I
A3: The
A4: Have
And in cell B1, I have this sentence:
"Until further notice, I have closed the Icecream store"
In cell C1, I want it to return:
"Further Notice, Closed Icecream Store"
Currently, I've been using the =SUBSTITUTE() function over and over like this:
=PROPER(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B1, $A$1, ""), $A$2, ""),$A$3, ""), $A$4, ""))
However with that formula, the "I" in "Icecream" will be deleted, and the formula itself will have to become ridiculously long in order to accommodate hundreds of cells in Column A.
Another option I've found that is equally as clunky is to use the =SPLIT() function on cell B1 to separate each word of the string into it's own cell (cells C1:Z1), then using:
=IF(ISNUMBER(SEARCH(C1,$A$1:$A,1)),"",C1)
=IF(ISNUMBER(SEARCH(D1,$A$1:$A,1)),"",D1)
etc...
In cells AA1:AZ1 to delete the unwanted words. And then finally in Cell BA1, a =CONCATENATE() formula to put all the cells back together into the desired string. This formula will also have issues with the "I" in "Icecream", so the only workaround for that I've found is putting spaces before and after each word in Column A, and adding spaces before and after the string in B1.
I'm curious if there's a better way to approach this problem that doesn't either require writing an essay long formula, or using thousands of cells and formulas to separate each word and check individually.
Thanks!
Tyler
With the Google Sheets REGEXREPLACE and JOIN Functions, sample regular expression can be:
(?i)\b(Until|I|Have|The)\b
where (?i) is an ignore case flag, \b is word boundary, and () is a capturing group.
=PROPER(TRIM(REGEXREPLACE(B1, "(?i)\b(" & JOIN("|", A1:A4) & ")\b", "")))
The Trim function also removes extra spaces between words.
Excel 2016 has TextJoin Function, but no RegEx Functions yet (VBA UDF can be used instead).
Microsoft Word has similar but limited pattern matching using wildcards, but joining the pattern in Word would be too challenging. Sample wildcards replace pattern in Word : <(Until|I|Have|The)>
Consider the following UDF():
Public Function NoJunk(r1 As Range, r2 As Range) As String
Dim ary(), r As Range
temp = " " & r1.Text & " "
ReDim ary(1 To r2.Count)
i = 1
For Each r In r2
ary(i) = " " & r.Text & " "
i = i + 1
Next r
For i = 1 To r2.Count
temp = Replace(temp, ary(i), " ")
Next i
NoJunk = Application.WorksheetFunction.Trim(temp)
End Function
For example:
Because the comparisons are case sensitive, I have included both lower case and proper case in column A.
User Defined Functions (UDFs) are very easy to install and use:
ALT-F11 brings up the VBE window
ALT-I
ALT-M opens a fresh module
paste the stuff in and close the VBE window
If you save the workbook, the UDF will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the UDF:
bring up the VBE window as above
clear the code out
close the VBE window
To use the UDF from Excel:
=nojunk(B1,A1:A8)
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work!
If it is less then ~10 or so unique words which you know what is to be deleted, you could just copy the column, Ctrl + H and Find/Replace that word with nothing and repeat until you've removed them all.

What is wrong with my formula?

I am trying to record a Excel macro that will be conducting this action:
I have 2 worksheets. 'Worksheet' and 'Report 1'. The scenario I am seeking for is,
"If Report 1 AB column is blank, Worksheet F column will use the data in >Report 1 U column. If it has text, Worksheet F column will use data in Report 1 > AC column."
I have tried out different formulas. At first I used the whole column range as formula, putting blank as 0 and also trying " ". But the result I have is 0. I also tried to remove ranges and use one cell but it still does not work. So what is wrong with my formula?
=IF('Report 1'!AB5=" ", Worksheet!F6='Report 1'!AC5, Worksheet!F6='Report 1'!U5)
In the first place, is my formula even correctly structured if I want to achieve the scenario above?
As one of the other commenters mentioned, you're testing for a "space" by typing
" "
Instead, use
""
i.e. the two quotation marks with no character in between.
=IF('Report 1'!AB5="",'Report 1'!U5,'Report 1'!AC5)
Place above formula into cell F6 at sheet "Worksheet"
Please try.
edited answer below:
=IF('Report 1'!AB5="",'Report 1'!U5,'Report 1'!AC5)
if you have no text at all as your "blank".
=IF('Report 1'!AB5=0,'Report 1'!U5,'Report 1'!AC5)
if you want to set 0 as your "blank".
or... you can use both of it in 1 formula by using "or"
=IF(OR('REPORT 1'!AB5=0,'REPORT 1'!AB5=""),'REPORT 1'!U5,'REPORT 1'!AC5)
or...
=IF(ISBLANK('Report 1'!AB5),'Report 1'!U5,'Report 1'!AC5)

Reference combined cell above

I have a sheet with a lots of columns ordered in a hierarchical way with the cells merged:
I'd like to name those columns (in example: row 5) like this:MainGroupA-SubGroupA-SubSubGroupA.
Simply referencing the columns above in the classic way won't work as the field above isn't available anymore. (In the example: the fields B1 to F1) (i.e. I can't enter A1&A2&A3 / R[-4]C&R[-3]C&R[-2]C as this formula tries to read from the "hidden" cells).
Is there a way to do this without manual work or the need to un-merge the parent-cells? I might be able to do this with some external text editor or even VBA but would prefer an "Excel formula solution" as it would stay updated for new groups and columns.
To Clarify: I'd like all columns in Line 5 to have the text like in A5
If you want:
MainGroupA-SubGroupA-SubSubGroupA
in A5 then this should work:
=A1&"-"&A2&"-"&A3
Edit Then try:
=OFFSET(A1,0,1-MOD(COLUMN(),6))&"-"&OFFSET(A2,0,MOD(COLUMN(),2)-1)&"-"&A3
though this won't give the same text as in A5 across the complete row.
The answer from pnuts is great and helped me solve some test cases. It was however a little difficult to adapt and produced empty strings for the last column, so I also wrote a VBA-Function to do exactly what I need.
Open the VBA Editor (ALT + F11) and enter the following code in a new module:
Public Function checkLeftIfEmpty(start As range) As String
If start.Cells.Count > 1 Then
checkLeftIfEmpty = "Only a single cell allowed as parameter"
Exit Function
End If
Dim currentRange As range
Set currentRange = start
Do While currentRange.Column >= 1
If currentRange.Value <> "" Then
checkLeftIfEmpty = currentRange.Value
Exit Function
Else
Set currentRange = currentRange.Offset(0, -1)
End If
Loop
End Function
You can now use the function checkLeftIfEmpty to find the first cell left-side from your parameter which contains text: (This will be the text of the merged cell itself, if applied to a "hidden by merge" cell)
And also in combination to concatenate a string:

Resources