Concatenate different columns values in a cell - excel

Let's supose 'large' excel like this:
A |B
ab |ef
|oo
ut |
|oo
ut |ef
That I need is a new row with a summary of all differents values of each column:
A |B
ab |ef
|oo
ut |
|oo
ut |ef
ab,ut |ef,oo <- new row with the 'summary'
Note: I can copy by hand the formula at end of each column, I only need the formula

Following is a function that can be used to Concatenate unique columns values in a cell
Function UniqueItem(InputRange As Range) As Variant
Dim cl As Range, cUnique As New Collection, cValue As Variant
Application.Volatile
On Error Resume Next
For Each cl In InputRange
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
End If
Next cl
UniqueItem = ""
For i = 1 To cUnique.Count
If UniqueItem = "" Then
UniqueItem = UniqueItem & cUnique(i)
ElseIf UniqueItem <> "" Then
UniqueItem = UniqueItem & ", " & cUnique(i)
End If
Next
On Error GoTo 0
End Function
How to use this function
1. Open excel file
2. Press Alt + F11
3. Create a new module and paste the code in it
4. Go back to the excel file and select the cell you want to have the result
5. Enter formula as =UniqueItem(A1:A5) A1:A5 specifies the range. You can specify any range.
Please find the sample file at the following link: Concatenate_different_columns_values_in_a_cell.xlsm

With some tricks (and an extra column) you can do this the following way.
In column A row 1 through 6, I placed some random text.
In column B I placed the following formulae.
B1: =IF(COUNTIF($A$1:$A1,A1)=1,A1&", ","")
B2: =IF(COUNTIF($A$1:$A2,A2)=1,A2&", ","")
B3: =IF(COUNTIF($A$1:$A3,A3)=1,A3&", ","")
B4: =IF(COUNTIF($A$1:$A4,A4)=1,A4&", ","")
B5: =IF(COUNTIF($A$1:$A5,A5)=1,A5&", ","")
B6: =IF(COUNTIF($A$1:$A6,A6)=1,A6&", ","")
B7: =SUBSTITUTE(B1&B2&B3&B4&B5&B6&",",", ,","")
The idea here is that the search list grows from row 1 to the end and marks any unique value. If the statement in B1 through B6 is true, the value from A is used and a , {space} is added.
In B7 I just concatenate all values, then I add an extra comma at the end.
The substitute removes the last ,{space}, with nothing, effectively making sure the list does not end with a comma.
You can paste the formula for B1 in cell B1 and then just paste the formula down. The relative reference automatically increases the search array.
Understand. One (not so elegant) way is the following... Set C1: = B1, the C2: =B1&B2, then copy C2 all the way down to the end.... On the other hand, I think this might be better served by a VBA solution.
Alternatively, you could use this...
http://mcgimpsey.com/excel/udfs/multicat.html
Regards,
Robert Ilbrink

Related

In Excel formula cell, is there a way to programmatically change the expression from using default variables to customized cell names?

I have an Excel sheet with thousands of formula cells. For example,
E1=A1+B1+C1*D1, E2=A2+B2+C2*D2, E3=A3+B3+C3*D3, E4=A4+B4+C4*D4, ....
.... E5000=A5000+B5000+C5000*D5000
I used this code below to change the names of the relevant cells.
colV = 1
For c = cFrom To cTo
Rowv = 1
For r = row1 To row5000
With Worksheets(ActiveSheet.Name)
Worksheets(ActiveSheet.Name).Cells(r, c).Name = ActiveSheet.Name & "R" & Rowv & "C" & colV
Rowv = Rowv + 1
End With
Next r
colV = colV + 1
Next c
After the execution of the above code, the new names for A1, B1, C1, D1 and E1 are now Sheet1R1C1, Sheet1R1C2, Sheet1R1C3, Sheet1R1C4, Sheet1R1C5, respectively, which is expected and correct. The same rule applies to succeeding rows.
But when I see the formulas in the formula bar in all rows in column E, the original/default expression is retained. For example the formula of the cell E1 is still =A1+B1+C1D1, E2 is still =A2+B2+C2D2 and so on. Is there a way to express the formula in terms of the new name of the cells programmatically? For example, the E1 should be expressed as = Sheet1R1C1+Sheet1R1C2+Sheet1R1C3Sheet1R1C4,
E2 should be expressed as = Sheet1R2C1+Sheet2R2C2+Sheet1R2C3Sheet1R2C4 and so on. This is needed because the new expression using the updated cell names will be stored in the csv.
The
FormulaVar = Worksheets(ActiveSheet.Name).Cells(r, c).Formula
yields original expression
I appreciate your help.
Select Formulas > Define Name > Apply Names
In the dialog that pops up select all the names and press OK:
Your =A1+B1+C1 should now read =Sheet1R1C1+Sheet1R1C2+Sheet1R1C3
The VBA for this is below, but will throw an error if there aren't any references to find.
Sub Test()
Cells.ApplyNames Names:=Array("Sheet1R1C1", "Sheet1R1C2", "Sheet1R1C3", "Sheet1R1C4")
End Sub
After updating the formula then the code Debug.Print Cells(1, 4).Formula returned =Sheet1R1C1+Sheet1R1C2+Sheet1R1C3.

Split the data in a column excel

I have a column that contains data for eg: 6B31-21045M22-AA
I'm trying to split the data before and after '-'. Like
A B C D
6B31-21045M22-AA 6B31 21045M22 AA
I tried
=LEFT(A2, SEARCH(“-”,A2)-1) and =Right(A2, SEARCH(“-”,A2)-1)
but if "-" occurs more than once then how do i split the 6B31-21045M22-AA or
6B31-21045M22-AA-SWQ
You don't need a formula or a VBA script. You can use Text to Columns function. Simply select the column with your data and use Text to Columns button on the Data panel. Then in a Wizard select Delimiter and set a "-" symbol as a delimiter.
With data in A1, in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,"-",REPT(" ",999)),COLUMNS($A:A)*999-998,999))
and copy across:
(this can also be done with VBA)
EDIT#1:
Using VBA, select the cell you wish to process and run this:
Sub Splitter()
Dim ary, i As Long, a
With ActiveCell
ary = Split(.Value, "-")
i = 1
For Each a In ary
.Offset(0, i).Value = a
i = i + 1
Next a
End With
End Sub

Want to get multiple values from an index array into a single cell in excel

I am trying to find a way to get multiple values from an array to display in one cell
Say for example I have the two columns as below
a 1
b 2
c 1
d 3
e 2
I want all the values form the first column where the second column is 1
vlookup and index with match both only provide the first matching instance, is there a way to do this with a function or does it have to be created in a macro with VBA?
Thanks
If you want all the results to be shown in ONE cell you could use that VBA formula:
'r is the range of cells that you have a value to look for
'v is the value you are looking for
Function getValues(r As Range, v As Variant)
Dim c As Range
getValues = ""
For Each c In r
If c.Value = v Then
If getValues = "" Then
'Offset(0,-1) will give you value from previous coulmn
getValues = c.Offset(0, -1).Value
Else
getValues = getValues & "," & c.Offset(0, -1).Value
End If
End If
Next c
End Function
Use example: in cell C1 enter this =getValues(B1:B5,1)
Considering your data is in range A1:B5 and Cell C1 contains the number you are looking for.
Enter the following array formula in Cell D1 and drag/copy down till the row as required.
=IFERROR(INDEX($A$1:$A$5, SMALL(IF($C$1=$B$1:$B$5, ROW($B$1:$B$5)-MIN(ROW($B$1:$B$5))+1, ""), ROW(A1))), "")
Being an array formula you'll have to commit above formula by pressing Ctrl+Shift+Enter.
Using helper columns this is possible with formulas alone.
Formulas:
In D1:
=""
In D2 downwards:
=IF(B2=$B$1,D1&A2&", ",D1)
In C1:
=LEFT(LOOKUP(2,1/(D:D<>0),D:D),LEN(LOOKUP(2,1/(D:D<>0),D:D))-2)

Search and return text from column in Excel

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.

set sum formula in excel using vba

Working on a excel macros where I am trying to add the cell values from above cells to calculate total value. This is what my data looks like
Here I want to add above cell values for each column to calculate the sum. To accomplish this i have written a macro as follows.
For cl = 2 To 5
Worksheets(5).Cells(4, cl).Formula = "=SUM(B4:B6)"
Next cl
This should set the formula to each cell in a row till 5 columns.
But it sets the same formula on all cells in a row it should get change according to column. How to set sum formula for each cell for corresponding column ?
Not sure I quite understand your code. You seem to be writing into row 4, but you also want to sum from row 4 to row 6. That will create a circular reference.
Let's assume the formula is written into row 3 instead. You will want to use the R1C1 reference style to make the cells to sum relative to the current cells.
A trick to learn what reference to use is:
Get a new worksheet, enter =SUM(B4:B6) into cell B3 and copy to the right.
Then click File > Options > Formulas and select R1C1 Reference Style.
Now inspect the formulas in the sheet. You will see this: =SUM(R[1]C:R[3]C)
This is what you need in the macro.
For cl = 2 To 5
Worksheets(5).Cells(3, cl).FormulaR1C1 = "=SUM(R[1]C:R[3]C)"
Next cl
some more details to R1C1 and R[1]C[1] style in formulas.
As far as I know R[1]C[1] creates a relative reference and R1C1 creates a absolute reference. But keep in mind that the figures for R[x] and C[y] are an offset to the cell which contains the formula.
That means if you want to show the sum of A1:B4 in C5 the code has to be like this:
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R[-4]C[-2]:R[-1]C[-1])"
If you want to the same but ending up with an absolute reference is aht to look like this.
Worksheets(5).Cells(5, 3).FormulaR1C1 = "=SUM(R1C1:R4C2)"
You can use very simple formula like below:
Sub sum_month()
Sheets("13").Activate
Range("D2").Formula = "=SUM(A1+A2+A3)"
End Sub
And next just hold and drag the cell to fill up another rows automatically.
For your case, you can use this:
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(0, 0), (Cells(1, cl).Column < 27) + 2)
ValueSum = "=SUM(" & ColName & "4:" & ColName & "6)"
Worksheets(5).Cells(4, cl).Formula = ValueSum
Next
Try something like this.
For cl = 2 To 5
ColName = Left(Cells(1, cl).Address(False, False), 1 - (cl > 26))
Worksheets(5).Cells(4, cl).Formula = "=SUM(" & ColName & "4:" & ColName & "6)"
Next cl
I think you can just reference the whole formula range and Excel will be smart enough to adjust the columns.
With no loop:
Worksheets(5).Cells(4, cl).resize(1,4).Formula = "=SUM(B4:B6)"

Resources