How can i concatenate row text with comma in excel? - excel

I have an excel data as follow and i want to concatenate skills that seperated by comma according by person name like this;
Irakli Beridze | C#, Python, Java
Parpali Zurashvili | C++, C
I can achieve according to followed sceanorio but i have n pieces data row.

You can do it using a helper column like this:
Insert filters and sort by name:
In cell C2, put the formula:
=IF(A2=A3,0,1)
0 will be where the 'copies' will be and 1 will be where the final line to be kept will be.
In cell D2, put the value of B2, and in D3, put the following formula:
=IF(A3=A2,D2&", "&B3,B3)
Now that that's done, copy and paste values in column D (Copy whole column, use paste special and pick 'Values'). Remove the filter, add back the filter but this time on all 4 columns and filter on 0 in column C:
Delete those rows and clear the filters. Finally, sort column A:
You can now delete columns B and C.

If you don't mind VBA you can use the following:
Sub ConcatRows()
Dim arr As Variant
Dim i As Long
Dim d As Dictionary
'Create a dictionary to hold all Name and Skill Values
Set d = CreateObject("Scripting.Dictionary")
'Fill an array with all Values
arr = Range("A2", Cells(Rows.Count, 2).End(xlUp))
'Loop the Values and and them into a dictionary
For i = LBound(arr) To UBound(arr)
'If Name already in list then Add Skill to Item value of Name Key
If d.Exists(arr(i, 1)) Then
d(arr(i, 1)) = d(arr(i, 1)) & ", " & arr(i, 2)
'If Name isn't already in list then add name with its first Skill
Else
d.Add arr(i, 1), arr(i, 2)
End If
Next i
'Write all Name back to Worksheet
Range("A2").Resize(d.Count) = Application.Transpose(d.Keys)
'Write all Skills Back to worksheet
Range("B2").Resize(d.Count) = Application.Transpose(d.Items)
End Sub

Related

Send all items from the listbox to worksheet table

How can I send all items from the listbox to worksheet table?
Tried this code but it's not working.
Worksheets("RIS").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = lbCart.List(i)
ListBox named "lbCart"
worksheet "RIS" and table named "Items"
To become like this
I notice that your lbCart columns don't align with those on your spreadsheet, so you need a way to write the data to the specific sheet columns of your template. This makes the task slightly more complicated than the usual copy list to variant, copy variant to range process:
Dim x
x = Me.lbCart.List
Range("C2").Resize(UBound(x) + 1, UBound(x, 1)).Value = x
I came up with this - which will allow you to define where each column goes by cycling through each cell:
columnarray = Array("C", "B", "D") ' first column writes to C, then B, then D
With Worksheets("RIS")
For c = 0 To UBound(columnarray)
For r = 0 To lbCart.ListCount - 1
Range(columnarray(c) & "2").Offset(r).Value = lbCart.List(r, c)
Next
Next
End With

Matching the data across column and rows using VBA

I have two sheets :
Sheet 1 consist of :
Sheet 2 consist of :
And the output should show in M column in Sheet1. I am attaching the sample output here :
So,what I have here is ID in Sheet 1, for eg : ID 'US' has Abhay,Carl and Dev
and in Sheet3, I have names in column and ID in Rows.
What i want is my Sample output column should populate using macro based on matched values from Sheet3
I am using below logic but something is going wrong :
For i = 2 To 10
j = i + 1
If ThisWorkbook.Sheets("Input").Range("N" & i) = ThisWorkbook.Sheets("Sheet3").Range("A" & i) And ThisWorkbook.Sheets("Input").Range("K" & i) = ThisWorkbook.Sheets("Sheet3").Range("B1") Then
ThisWorkbook.Sheets("Input").Range("O" & i) = ThisWorkbook.Sheets("Sheet3").Range("B" & j)
End If
Next i
Since you asked for a VBA solution, please see the code below.
Dim colLen As Integer
Dim i As Integer
Dim colPt As Integer
Dim rowPt As Integer
' Counts number of rows on Sheet 1, column B.
colLen = Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Loops through all names on Sheet 1.
For i = 2 To colLen
' Retain US or NA ID for blank cells.
If Sheets(1).Cells(i, 1) <> "" Then
If Sheets(1).Cells(i, 1) = "US" Then
colPt = 2
Else
colPt = 3
End If
End If
' Find name on Sheet 2 and set row.
rowPt = Sheets(2).Range("A:A").Find(Sheets(1).Cells(i, 2)).Row
' Add ID from Sheet 2 to Sheet 3
Sheets(1).Cells(i, 3) = Sheets(2).Cells(rowPt, colPt)
Next i
Assumptions:
Sheet 1 is the main worksheet, sheet 2 has the lookup data.
All names in the lookup data are unique.
I would recommend including the ID in every row instead of treating it as a heading but that's preference. There are formula solutions that would work for this as well if you want to skip VBA.
There are a few ways to approach this. Below is one of them:
NOTE: for simplicity, I have kept my data on one sheet. You can amend the below formulas as your data is on 2 sheets. Saying that, I have used the same columns as you have in your query
Solution:
Have a "holding column". In my example, I used column J as the holding column (you can hide this column if you want). In J2, type the following formula: =IF(ISBLANK($K2), $J1,$K2). Copy the formula down to all used rows. Then copy the following formula in M2: =VLOOKUP($L2,$A$3:$C$8,IF($J2="US",2,3),FALSE). As per before, copy the formula down to all used rows. This should give you your results

Dynamic Range Based on User Input

In excel I am creating a dynamic array. Column A contains a key, using numbers 1-7. Columns B-K contain strings. The array is made by looping from 1 to the last row and for each loop there is an if statement checking the key to see if it is on the right row. Inside the if statement the value of column B is added to the array. I have working code for this, however I need the range that gets the value in column B to be dynamic so the code will have the same result except it will work for any row, based on user input, and not just column B. I have a dynamic userform list of radio buttons at the beginning of the macro that contains all of the column headers. The user selected column head (string value) is stored in "SelectedOption".
Below is my current code, any help making the "Range("B" & i)" part dynamic based on the user input would be appreciated.
For i = 1 To lRow
If Range("A" & i) = 4 Then
ArrayTest(UBound(ArrayTest)) = Range("B" & i) 'this needs to be a dynamic range to get correct values in array
ReDim Preserve ArrayTest(UBound(ArrayTest) + 1)
End If
Next i
Solved by declaring new range using cells(row,column). Finished code below:
For i = 1 To lRow
If Range("A" & i) = 4 Then
Set arrayRange = Cells(i, SelectedColumn)
ArrayTest(UBound(ArrayTest)) = arrayRange
ReDim Preserve ArrayTest(UBound(ArrayTest) + 1)
End If
Next i

In Excel VBA, extract range text and sum data

I have a spreadsheet in which there are multiple rows that have three columns (K, L M) that contain text (inserted manually from a dropdown). The inserted text includes a 'score'. For the row shown in the image that score is 3 + 2 + 2 = 7.
What I'd like to be able to do is to have that score automatically calculated and shown in column N. I'm happy to do the score extraction given the text, but I'm completey unfamiliar with Excel's object model, and how to write a VBA macro that can be triggered across all of the rows. I assume it would be passed a range somehow, or a string designating a range, but how to do that is beyond me just now. Perhaps I just need a formula? But one that calls a function to strip non-numerical data from the cell?
Any help appreciated.
Put this formula in N2 cell and drag it all the way down.
=LEFT(K2, FIND("-", K2) - 2) + LEFT(L2, FIND("-", L2) - 2) + LEFT(M2, FIND("-", M2) - 2)
For more information see reference. It sum all numbers, that are present before the hyphen (-) in a cell.
Try:
N2 = LEFT(TRIM(K2),1) + LEFT(TRIM(L2),1) + LEFT(TRIM(M2),1)
As I said in comments, this solution does not scale so well if it is more than three columns and / or the scores are more than single digit [0-9]
A VBA solution to do all of your rows and enter the values into Column N:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'get the last row with data on Column A
For rownumber = 1 To LastRow 'loop through rows
For i = 11 To 13 'loop through columns
strValue = ws.Cells(rownumber, i).Value 'get text string from cell
pos = InStr(strValue, " -") 'find the dash - in cell
If pos > 0 Then 'if dash found
Value = Value + Val(Left(ws.Cells(rownumber, i).Value, pos - 1)) 'remove everything after number
End If
Next i
ws.Cells(rownumber, 14).Value = Value 'write value to column N
Value = 0
Next rownumber
End Sub

EXCEL vba - extract numbers from cell and paste into two different columns?

I have a spreadsheet with a load of random text and numbers in column A like so:
Column A
Row 1 = 471806121601 5205569 - 0007 Standard White Toilet Tissue 27
Row 2 = 471814121601 5206177 - 0014 Premium White Toilet Tissue 6
Row 3 = 471814121601 5206178 - 0007 Premium White Toilet Tissue 27
Row 4 = 471806121601 5206180 - 0014 Premium Kitchen Towel 2x75l 6
I have about 2000 lines in total. In each cell, is a Purchase order number (12 digits) and an item number next to it (7 digits).
I am trying to extract the po number and put it into column B and extract the item number and put it into column C
Column B Column C
471806121601 5205569
471814121601 5206177
471814121601 5206178
471806121601 5206180
Here is my code:
Option Explicit
Sub main()
Dim cell As Range
Dim arr As Variant, arrElem As Variant
With Worksheets("Orders") '<--| change "Strings" to your actual worksheet name
For Each cell In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
arr = Split(Replace(cell.Value, " ", " "), " ") '<--| change "A"'s to your actual relevant column index
For Each arrElem In arr
If IsNumeric(arrElem) Then
If Len(arrElem) = 12 Then cell.Offset(0, 1).Value = arrElem
End If
Next arrElem
Next cell
End With
Dim cell2 As Range
Dim arr2 As Variant, arrElem2 As Variant
With Worksheets("Orders") '<--| change "Strings" to your actual worksheet name
For Each cell2 In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
arr2 = Split(Replace(cell2.Value, " ", " "), " ") '<--| change "A"'s to your actual relevant column index
For Each arrElem2 In arr2
If IsNumeric(arrElem2) Then
If Len(arrElem2) = 7 Then cell2.Offset(0, 3).Value = arrElem2
End If
Next arrElem2
Next cell2
End With
End Sub
This code does work. However it takes absolutely ages and only does one line at a time...Slowly.
Is there a quicker way of doing this? Thanks
If your PO and IN are always the same length in col B put
=MID(A2, 1, 12)
And in col C
=MID(A2, 14, 7)
However if your number change but are always the first two swap the above for,
=MID(A2,1,FIND(" ",A2,1)-1)
And
=MID(A2, FIND(" ", A2, 1)+1, 7)
Respectively.
just use split(string,delimiter)(0) and (1) why replace the space, just use that as the delim. If Row # is in, then use (1) and (2), or you could consider split(split(input,"-")," ") maybe a little faster, not sure though. Also, once you're done no need to complete the loop, so consider, do until with flags rather than for next, although exit for is available
Formula wise, it could be done using something like this
=MID(D1,FIND("é",SUBSTITUTE(D1," ","é",3)),FIND("é",SUBSTITUTE(D1," ","é",4))-FIND("é",SUBSTITUTE(D1," ","é",3)))
and
=MID(D1,FIND("é",SUBSTITUTE(D1," ","é",4)),FIND("é",SUBSTITUTE(D1," ","é",5))-FIND("é",SUBSTITUTE(D1," ","é",4)))

Resources