Excel - formula link cells based on another column - excel

I want to use a formula (or a function I'll make in VBA, I prefer a formula) to do the following:
I have text in column A, that I want to link in Column E based on similar cells in column B.
For example: cell B2,B3,B4 equals "A" and I want cell E2 to be the values in column A with a ";" between the values.

I am not sure how to do it using a formula but below VBA method will work.
Press Alt + F11 in the worksheet to open the VBA editor window
In the left pane, Right click sheet1 and Insert module
Copy paste the below method
Use the method MultipleLookupNoRept in the formula
Function MultipleLookupNoRept(Lookupvalue As String, LookupRange As Range,
ColumnNumber As Integer)
Dim i As Long
Dim Result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = Lookupvalue Then
For J = 1 To i - 1
If LookupRange.Cells(J, 1) = Lookupvalue Then
If LookupRange.Cells(J, ColumnNumber) = LookupRange.Cells(i, ColumnNumber)
Then
GoTo Skip
End If
End If
Next J
Result = Result & " " & LookupRange.Cells(i, ColumnNumber) & ";"
Skip:
End If
Next i
MultipleLookupNoRept = Left(Result, Len(Result) - 1)
End Function

You can use an array formula for this:
=TEXTJOIN(";",TRUE,IF(B:B=B2,A:A,""))
Enter the formula in the bar in cell E2 and press ctrl+shift+enter to create an array formula.

Related

excel vlookup multiple rows into one cell

I would like to get the data from several rows from one sheet sheet1 into a single cell on another sheet sheet2 based on a lookup.
For example there is data on one sheet :
sheet1
And I would like to lookup data based on id and return all the concerning rows into one cell like this:
sheet2
Is that possible with an excel formula or is this only solvable with VBA?
Thank you for your help in advance.
I found a vba that came close to a solution but didn't work. I've looked at "index, match" functions "small" functions but could find a solution that puts data into a single cell...
This is the vba code I found that came close to solution:
'Function SingleCellExtract(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer)
Dim i As Long
Dim Result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = Lookupvalue Then
Result = Result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
Next i
SingleCellExtract = Left(Result, Len(Result) – 1)
End Function'
the vba threw value or compile errors.. it looks like it only returns values from one vertical column
"Is that possible with an excel formula or is this only solvable with VBA?"
It sure is possible through formula, but you'll have to have access to the TEXTJOIN function:
Formula in H2:
=TEXTJOIN(CHAR(10),TRUE,IF($A$2:$A$11=G2,$B$2:$B$11&", "&$C$2:$C$11&", "&$D$2:$D$11&", "&$E$2:$E$11,""))
Note: It's an array formula and need to be confirmed through CtrlShiftEnter
Drag the formula down and make sure you got textwrap selected on column H.
No access to TEXTJOIN? You can always create your own, for example:
Function TEXTJOIN(rng As Range, id As Long) As String
For Each cl In rng
If cl.Value = id Then
If TEXTJOIN = "" Then
TEXTJOIN = cl.Offset(0, 1) & ", " & cl.Offset(0, 2) & ", " & cl.Offset(0, 3) & ", " & cl.Offset(0, 4)
Else
TEXTJOIN = TEXTJOIN & Chr(10) & cl.Offset(0, 1) & ", " & cl.Offset(0, 2) & ", " & cl.Offset(0, 3) & ", " & cl.Offset(0, 4)
End If
End If
Next cl
End Function
In cell H2 you can call the UDF through =TEXTJOINS($A$2:$A$11,G2) and drag down. Again, make sure textwrapping is checked for the column.
EDIT:
As per OP's comment, this is how I got the data to show correctly:
Select column H and click textwrap + top alignment as shown in this screenshot:
Next, select all cells if result is not correct yet:
Double-click the line between columns and rows to space them to fit the data

Pass Multiple Values and Get Selected Results In A Cell

I am new with Excel VBA and trying to use it for a scenario in an Excel work book. I am trying to do a multiple value search in a cell and that should be highlighted. Say I've these ids - 1001, 1002, so in the specific cell these values should be highlighted or searched. I am not sure how can I use List<> in VBA but in C#, I can do the following:
List<string> aLst = new List<string>();
aLst.Add("1001");
aLst.Add("1002");
So with the list, I can iterate the ids and get the results matched. I was looking into the following VBA code and it gets one value as parameter. Then checks the matched one:
Function SingleCellExtract(Lookupvalue As String, LookupRange As Range, ColumnNumber As Integer)
Dim i As Long
Dim Result As String
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = Lookupvalue Then
Result = Result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
Next i
SingleCellExtract = Left(Result, Len(Result) – 1)
End Function
Here is the link that I am following: Excel VBA
So any way that I can use List<> and search the required values highlighted in the excel sheet?
Sample:
Id - Name
1001 John
1002 Jack
So copy this function into a new module.
Then you can access either via another function or procedure or through an excel formula in a cell like =MultiCellExtract(A2:A3;A2:B3;2)
' LookupValuesRange is an Excel Range of cells
Public Function MultiCellExtract(LookupValuesRange As Range, LookupRange As Range, ColumnNumber As Integer) As String
Dim cellValue As Range
Dim i As Long
Dim Result As String
For Each cellValue In LookupValuesRange
For i = 1 To LookupRange.Columns(1).Cells.Count
If LookupRange.Cells(i, 1) = cellValue.Value Then
Result = Result & " " & LookupRange.Cells(i, ColumnNumber) & ","
End If
Next i
Next cellValue
MultiCellExtract = Left(Result, Len(Result) - 1)
End Function
Let me know if it helps or we can adjust it.

VBA code to scan through a comma separated value in a cell and retrieve lookup value

I have a scenario where I have to read through values in one cell which is comma separated and retrieve only values from that array to match with a particular lookup value. For eg:
So what I need is a function to retrieve all Task(or any other issuetype which could vary) from row 2 Links column
expected result: Against A2 I want to retrieve A4 and A6
This is something I modified so that you could customize it to any lookup value
Function GetLinkedItem(link As String, targetLinkType As String)
Dim temp(0 To 0) As String
GetLinkedItem = "None"
If Trim(link) = "" Then Exit Function
Dim links() As String, i As Long
links = Split(link, ",")
For i = 0 To UBound(links)
'select the links that are targetLinkType
Dim j As Long
j = GetRow(Trim(links(i)))
If Sheets("Data").Cells(j, ISUUETYPE_COL) = targetLinkType Then
temp(0) = temp(0) & " " & Sheets("Data").Cells(j, ID_COL) & ","
End If
GetLinkedItem = Join(temp, ",")
Next i
End Function
You can create a UDF to perform this lookup. In a new module in your VBE, paste the following:
Function getTasks(tasklist As Range, availabletasks As Range) As String
'tasklist is the incoming array in Column C
'availabletasks is the stuff in Column A
'Array for output
Dim tasks() As String: ReDim tasks(0 To 0)
'Loop through each item in taslist using an array
For Each task In Split(tasklist.Value, ", ")
'Search availabletasks
If Not availabletasks.Find(task) Is Nothing Then
'pop the array
If tasks(0) <> "" Then ReDim Preserve tasks(0 To UBound(tasks) + 1)
tasks(UBound(tasks)) = task
End If
Next
'Return what we found
getTasks = Join(tasks, ", ")
End Function
Now in your spreadsheet you can use this function just like a regular formula:
=getTasks(C1,$A$1:$A$6)
Where C1 has the list like A4, A25, A22, A6, A29, A42 and $A$1:$A$6 are just like your example Column A. This will return A4, A6
Thanks so much. I added the code in a new module and used the function as formula. I was getting just 1 value instead of 2(Just got A4 and not A6).

get distinct list (index) if adjacent value equals a specific text in excel

I would like to get a distinct list of column J if cell in H of same row equals "No Company".
I tried =IFERROR(INDEX($J$2:$J$10, MATCH(0, COUNTIF($K$1:K2, $J$2:$J$10&"") + IF(ISTEXT($H$2:$H$10)="No company",1,0), 0)), ""))
also tried =IFERROR(INDEX(J$2:J$400, SMALL(IF((H$2:H$400=No company)>1)), ROW(j$2:j$400)-ROW(j$2)+1), ROWS(j$2:j2)), "") changing some of the fields but no luck
Desired result + Try
Create a new function:
open VBA Editor (alt + F11), add new module, insert code:
Function JoinAll(ByRef range As range, ByVal delimiter As String)
For Each c In range.Cells
If InStr(JoinAll, c.Value) = 0 Then
JoinAll = JoinAll + c.Value + delimiter
End If
Next
JoinAll = Left(JoinAll, Len(JoinAll) - Len(delimiter))
End Function
use formula like this:
=JoinAll(J2:J10, ", ")

Append string between each cell value in a series

Let's say I have a series of cells like so:
A
1 Foo
2 Bar
3 Hello
4 World
5 Random Text
What I'd like to do is have the result of my formula populate another cell with:
Foo, Bar, Hello, World, Random Text
Now, I know how to concatenate two cells with:
=A1&", "&A2
but how can I do the same thing with the entire series?
Here's a function you might be able to use. Simply put this in your workbook code module, then you can enter it in cells like:
=JoinRange(A1:A6) or =JoinRange(A2:D15), etc.
Public Function JoinRange(ByVal rng As Range) As String
Dim dlmt As String: dlmt = ","
Dim multiRow As Boolean: multiRow = rng.Rows.Count > 1
Dim r As Long, c As Long
Select Case rng.Columns.Count
Case 1
If multiRow Then
JoinRange = Join(Application.WorksheetFunction.Transpose(rng), dlmt)
Else:
'a single cell
JoinRange = rng
End If
Case Is > 1
If multiRow Then
'a 2d range of cells:
For r = 1 To rng.Rows.Count
For c = 1 To rng.Columns.Count
JoinRange = JoinRange & rng(r, c) & dlmt
Next
Next
JoinRange = Left(JoinRange, Len(JoinRange) - 1)
Else:
JoinRange = Join(Application.WorksheetFunction.Transpose( _
Application.WorksheetFunction.Transpose(rng)), dlmt)
End If
Case Else
End Select
End Function
Put a comma and a space in cell B1, then use this formula:
=CONCATENATE(A1,B1,A2,B1,A3,B1,A4, B1, A5)
There are several answers to the following question that you can try as well, including VBA options and a formula:
Need to concatenate varying number of cells...
With =A1 in B1 then =B1&", "&A2 in B2 and copied down would seem to work.

Resources