I know I can concatenate a set of cells using & like this:
A1 & B1 & C1 & D1. Is there a way to write this for a range A1:P1 using & ?
You can accomplish this with a formula in a series of columns (each sequentially concatenating from the previous) or with a UDF (User Defined Function).
Formula Method
The formula in F2 is,
=A2
The formula in G2 is,
=IF(LEN(B2), F2&","&B2, F2)
Fill G2 right to catch all of the available text values.
VBA UDF Method
Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the following into the new pane titled something like Book1 - Module1 (Code).
Function udf_stitch_Together(r As Range, Optional d As String = ", ") As String
Dim s As String, c As Range
For Each c In r
If CBool(Len(c.Text)) Then _
s = s & IIf(Len(s), d, vbNullString) & c.Text
Next c
udf_stitch_Together = s
End Function
Tap Alt+Q to return to your worksheet.
The syntax is simply,
udf_stitch_Together(<range to concatenate>, <[optional] delimiter>)
For your purposes this would be,
=udf_stitch_Together(A2:P2)
... or,
=udf_stitch_Together(A2:P2, ", ")
Fill down as necessary. Note that I have used =udf_stitch_Together(A5:P5, ";") in F5:F6 to demonstrate the ability to change the delimiter.
For a 1D range as you have (a single column or row), you could try this
For a Row, your example, it would be called like
=StrCat(A1:P1, TRUE)
Function StrCat(rng1 As Range, bRows As Boolean) As String
If bRows Then
'row
StrCat = Join(Application.Transpose(Application.Transpose(rng1)), ", ")
Else
'column
StrCat = Join(Application.Transpose(rng1), ", ")
End If
End Function
I know this is an old question but I found a way to use Concatenate like #brettdj was talking about. Using the method from this link you can join together a range using a combination of Concatenate and Transpose like so:
Let say the cells you want to combine are in B2:B19.
In a blank cell, where you want to concatenate all the values type =CONCATENATE(TRANSPOSE(B2:B19))
Don’t press enter yet.
Select the TRANSPOSE(B2:B19) portion and press F9. This replaces the TRANSPOSE(B2:B19) with its result
Now remove curly brackets { and }
Press Enter & you're Done!
Demo from link:
(source: chandoo.org)
Related
Is it possible to get all the values from Table 1 corresponding to the values in the cell of Table 2 without using VBA ?
For example.
In table 2 one value is "India Australia" (F3). I need to get the result as "IND AUS" looking from the table 1.
Thanks in advance!
Using TEXTJOIN:
=TEXTJOIN(" ",TRUE,VLOOKUP(FILTERXML("<a><b>"&SUBSTITUTE(F3," ","</b><b>")&"</b></a>","//b"),A:B,2,FALSE))
If one does not have TEXTJOIN then VBA is the only way to get it in one cell. See Here for a UDF that mimics TEXTJOIN: VLOOKUP with multiple criteria returning values in one cell
1] For Office 365 and Excel 2019 user, to use CONCAT function
In G3 array formula copied down :
=CONCAT(IF(ISNUMBER(SEARCH($A$3:$A$6,F3)),$B$3:$B$6,"")&" ")
This is an array formula and needs to be confirmed by pressing with Ctl + Shift + Enter.
2] For all Excel version user, try this longer formula
Create a range name
Select G3 >> Define name >>
Name : Abb
Refers to : =IF(ISNUMBER(SEARCH($A$3:$A$6,$F3)),$B$3:$B$6,"")
OK >> Finish
Then
In G3, enter formula and copied down :
=TRIM(IFERROR(INDEX(Abb,1),"")&" "&IFERROR(INDEX(Abb,2),"")&" "&IFERROR(INDEX(Abb,3),"")&" "&IFERROR(INDEX(Abb,4),"")&" "&IFERROR(INDEX(Abb,5),""))
I borrowed this VBA solution from extendoffice.com. It's very simple, easy to set up, and worked really well for my situation.
My scenario -
Sheet 1 contains the lookup value in column A where I want the formula results in column B.
Sheet 2 contains the table array in columns A and B where column A is again the lookup value and column B contains the values to be returned in the results.
First, press ALT+F11 to bring up the VBA editor.
Second, paste this Function into Module1 within the VBA editor:
Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
'Updateby Extendoffice
Dim rng As Range
Dim xResult As String
xResult = ""
For Each rng In pWorkRng
If rng = pValue Then
xResult = xResult & ", " & rng.Offset(0, pIndex - 1) 'I changed EO's delimiter from a space to a comma+space.
End If
Next
If Left(xResult, 2) = ", " Then
xResult = Right(xResult, Len(xResult) - 2) 'This removes any leading commas that make it into the results.
Else
'All is well in the world!
End If
MYVLOOKUP = xResult
End Function
Third, double click on the cell you want the multi-results in and paste this formula.
=MYVLOOKUP(A2, 'SheetName'!A1:B1816, 2)
Fourth, edit the formula to fit your spreadsheet.
A2 - The lookup value that will be compared against the other list.
'SheetName'!A1:B1816 - Replace SheetName with the name of the sheet your other list is on and replace A1:B1816 with the full range of values that are both being looked up and returned.
2 - If the values you want returned in your results are in a column other than B, replace '2' with the column that contains the values you want returned in your results.
You're done! If you did everything correctly (and you don't have any Trust Center settings blocking VBA code) then it should look like a VLOOKUP returned multiple results in a single cell.
How to create a formula in result column based on the values of the previous column
if values in any previous columns (from c1 to c5) is greater than zero, I want to add it to the result.
If you happen to have access to TEXTJOIN function you can use the following:
Formula in F2:
=TEXTJOIN(",",TRUE,IF(A2:E2>0,$A$1:$E$1,""))
Note 1: It's an array formula and needs to be confirmed through CtrlShiftEnter
Note 2: Without access to TEXTJOIN this will become a much more painfull process, but for just the 5 columns you can try =IF(A2>0,A$1&",","")&IF(B2>0,B$1&",","")&IF(C2>0,C$1&",","")&IF(D2>0,D$1&",","")&IF(E2>0,E$1&",",""), however you'll have to expand to get rid of the trailing comma
If both the above options are not feasible, your best bet will be to write a UDF. For this exercise, you can write a rather simple one:
Public Function TextJoin2(rng1 As Range, rng2 As Range) As String
With Application
Dim arr As Variant: arr = .Evaluate("IF(" & rng1.Address & ">0," & rng2.Address & ","" "")")
TextJoin2 = Replace(.Trim(Join(arr, " ")), " ", ",")
End With
End Function
Call in F2 like: =TEXTJOIN2(A2:E2,A$1:E$1) and drag down.
You can use =CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")) as array formula, confirm with ShiftCtrlEnter.
Put this formula in F2 cell in your example. Expand that formula to whole range
What remains here is to remove comma on the end of result string.
That full formula would be like this
=LEFT(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";""));LEN(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")))-1)
I need a solution for this:
A B
1. 1,2,3,4,5,,,
2.
3.
4.
5.
so i want to concatenate A column like this:
(A2;",";A3;",";A4;",";A5;",";A6;",";A7;","; and so on)
I want to remove commas (,) behind the number if they're less column - for example 5 and i concatenate 7.
How can i do that ?
there might be a better way of doing this but i will share my "dumb" way anyway.
In B1 i will make it = A1
In B2 =SUBSTITUTE(B1&","&A2,".","")
and i just need to fill down the column to the last row
you can get your final result at column B, last row
This is to prevent repetitive selecting all the cells in the whole list (using concatenate)
Use the function CONCATENATE as you said. You also can repeat that concatenate with the mouse for all rows.
=CONCATENATE(A2, ", ", A3) or =A2 & ", " & A3
For more information check this website:
Concatenate cells with a space, comma and other characters
I think, i find the simpliest & best solution and it's a vba code:
Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
For Each Cell In Ref
Result = Result & Cell.Value & Separator
Next Cell
CONCATENATEMULTIPLE = Left(Result, Len(Result) - 1)
End Function
Function: =CONCATENATEMULTIPLE(RANGE; ",")
CONCATENATE Excel Ranges (Using VBA)
I have an Excel worksheet and am trying to figure out the formula for the following:
With my formula, I want to search all 700 rows of Column A for cells that contain aa_product11.12
Column A's value may or may not contain those values (some will, some will not). And, this is only the partial value. All of the cells have more data, i.e.
Column A
Sept01_aa_product11.12;
Oct01_aa_product11.12;
and so forth.
I need the full values of those cells that match, to show up in B1. So, the formula in B1 will search all of column A for aa_product11.12 and then B1 will look like:
Cell B1
Sept01_aa_product11.12, Oct01_aa_product11.12, Jan02_aa_product11.12,
Aug08_aa_product11.12
Thank you in advance for your help!
My answer will not immediately give you the concatenated results, but they are the step(s) before this:
Without formula:
Insert a filter and filter on Text with the criteria 'Contains aa_product11.12'.
Copy the column after the filter and paste the results in a fresh sheet.
You can then remove the filter and transfer the results back to the original sheet.
With a formula:
In B1, put the formula:
=IFERROR(INDEX($A$1:$A$700,SMALL(IF(ISNUMBER(FIND("aa_product11.12",$A$1:$A$700)),ROW($A$1:$A$700),9.9E+208),ROW())),"")
But this is an array formula which will require using Ctrl+Shift+ to work properly. After this, fill down the formula until there are no more results returned to proceed to the last step.
After doing either of these, you can run a CONCATENATE on the cells:
=CONCATENATE(B1,", ",B2,", ", ... )
Okay, You need to open your Visual Basic Editor and create a module. Place the following code in your module:
Function Get_Data()
Dim Strg As String
Dim Boo As Boolean
Boo = False
Dim i
i = 1
For i = i To 65000
Strg = Cells(i, 1).Value
If InStr(1, Strg, "aa_product11.12") Then
If Boo = True Then
Get_Data = Get_Data & Cells(i, 1).Value & ", "
Else
Get_Data = Cells(i, 1).Value & ", "
Boo = True
End If
End If
Next i
End Function
Save file as a Macro-Enabled workbook. Then go to cell B1 and type in: =Get_Data()
Press enter and the function should work. Let me know if you have any questions.
I have a single worksheet with sheets Sheet1 and Sheet2 and I am trying to reference a range of cells from Sheet2 to Sheet1
I know how to reference worksheet cells such as =Sheet2!A1 but how can I do the same for a cell range such as A1:F1 I tried =Sheet2!A1:F1 but it does not like the syntax.
I need to use Excel Formulas for this if possible.
Simple ---
I have created a Sheet 2 with 4 cells and Sheet 1 with a single Cell with a Formula:
=SUM(Sheet2!B3:E3)
Note, trying as you stated, it does not make sense to assign a Single Cell a value from a range. Send it to a Formula that uses a range to do something with it.
The formula that you have is fine. But, after entering it, you need to hit Control + Shift + Enter in order to apply it to the range of values. Specifically:
Select the range of values in the destination sheet.
Enter into the formula panel your desired formula, e.g. =Sheet2!A1:F1
Hit Control + Shift + Enter to apply the formula to the range.
Ok Got it, I downloaded a custom concatenation function and then just referenced its cells
Code
Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
Dim retVal, dlm As String
retVal = ""
If delim = Null Then
dlm = ""
Else
dlm = delim
End If
For Each cell In useThis
if cstr(cell.value)<>"" and cstr(cell.value)<>" " then
retVal = retVal & cstr(cell.Value) & dlm
end if
Next
If dlm <> "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function
If you wish to concatenate multiple cells from different sheets, and you also want to add a delimiter between the content of each cell, the most straightforward way to do it is:
=CONCATENATE(Sheet1!A4, ", ", Sheet2!A5)
This works only for a limited number of referenced cells, but it is fast if you have only of few of these cells that you want to map.
You can put an equal formula, then copy it so reference the whole range (one cell goes into one cell)
=Sheet2!A1
If you need to concatenate the results, you'll need a longer formula, or a user-defined function (i.e. macro).
=Sheet2!A1&Sheet2!B1&Sheet2!C1&Sheet2!D1&Sheet2!E1&Sheet2!F1
Its quite simple but not easy to discover --- Go here to read more. its from the official microsoft website
Step 1 -
Click the cell or range of the source sheet (that contains the data you want to link to)
Step 2
Press Ctrl+C, or go to the Home tab, and in the Clipboard group, click Copy Button image .
Step 3
Clipboard group on the Home tab
Step 4
Press Ctrl+V, or go to the Home tab, in the Clipboard group, click Paste Link Button. By default, the Paste Options Button image button appears when you paste copied data.
Step 5
Click the Paste Options button, and then click Paste Link .
I rewrote the code provided by Ninja2k because I didn't like that it looped through cells. For future reference here's a version using arrays instead which works noticeably faster over lots of ranges but has the same result:
Function concat2(useThis As Range, Optional delim As String) As String
Dim tempValues
Dim tempString
Dim numValues As Long
Dim i As Long, j As Long
tempValues = useThis
numValues = UBound(tempValues) * UBound(tempValues, 2)
ReDim values(1 To numValues)
For i = UBound(tempValues) To LBound(tempValues) Step -1
For j = UBound(tempValues, 2) To LBound(tempValues, 2) Step -1
values(numValues) = tempValues(i, j)
numValues = numValues - 1
Next j
Next i
concat2 = Join(values, delim)
End Function
I can't help but think there's definitely a better way...
Here are steps to do it manually without VBA which only works with 1d arrays and makes static values instead of retaining the references:
Update cell formula to something like =Sheet2!A1:A15
Hit F9
Remove the curly braces { and }
Place CONCATENATE( at the front of the formula after the = sign and ) at the end of the formula.
Hit enter.
If these worksheets reside in the same workbook, a simple solution would be to name the range, and have the formula refer to the named range. To name a range, select it, right click, and provide it with a meaningful name with Workbook scope.
For example =Sheet1!$A$1:$F$1 could be named: theNamedRange. Then your formula on Sheet2! could refer to it in your formula like this: =SUM(theNamedRange).
Incidentally, it is not clear from your question how you meant to use the range. If you put what you had in a formula (e.g., =SUM(Sheet1!A1:F1)) it will work, you simply need to insert that range argument in a formula. Excel does not resolve the range reference without a related formula because it does not know what you want to do with it.
Of the two methods, I find the named range convention is easier to work with.