# VLOOKUP Alternative? FINDING All References for a Value and Combining Them Into One Cell

#### kescco

##### Board Regular
Here is the scenario I have and I hope someone here can help.

In WORKSHEET 1 COLUMN A, I have the word DOG.
In WORKSHEET 2 COLUMN A, I have the word DOG four times.
In WORKSHEET 2 COLUMN B, I have COLLIE, GERMAN SHEPHERD, and POODLE.

What I would like to do is have EXCEL search Column A in WORKSHEET 2 for the word DOG, find all the related cells in COLUMN B WORKSHEET B, and combine them in a singe cell in COLUMN B of WORKSHEET 1.

So in COLUMN B of WORKSHEET 1, I would like to have a cell which has COLLIE, GERMAN SHEPHERD, and POODLE.

I am thinking CONCATENATE is likely to be used but is it more likely an ARRAY. Might someone know how to do this? If so, I would appreciate the help. I know how to take code and embed it into a worksheet and some basic VBA.

Thank You.

### Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What the data look a like, I mean your table in all worksheet?

kescco,

I assume that the two worksheets contain titles in row 1.

If both worksheets do not have titles in row 1, I will have to re-write the macro.

Sample worksheets:

Excel 2007
AB
1Title ATitle B
2DOGCOLLIE
3DOGGERMAN SHEPHERD
4DOGPOODLE
5DOG
6
2

Excel 2007
AB
1Title ATitle B
2DOG
3
1

After the macro in worksheet 1:

Excel 2007
AB
1Title ATitle B
2DOGCOLLIE, GERMAN SHEPHERD, POODLE
3
1

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
``````Sub FindUniques()
' hiker95, 09/17/2014, ME806189
Dim w1 As Worksheet, w2 As Worksheet
Dim c As Range, h As String, a As Range, lr As Long, n As Long, i As Long, sr As Long
Application.ScreenUpdating = False
Set w1 = Sheets("1")
Set w2 = Sheets("2")
lr = w2.Cells(Rows.Count, 1).End(xlUp).Row
With w1
For Each c In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
n = Application.CountIf(w2.Columns(1), c.Value)
If n > 0 Then
sr = 1
h = ""
For i = 1 To n Step 1
Set a = w2.Range("A" & sr & ":A" & lr).Find(c.Value, LookAt:=xlWhole)
If Not a Is Nothing Then
If w2.Cells(a.Row, 2) <> "" Then
h = h & w2.Cells(a.Row, 2) & ", "
End If
sr = a.Row
Set a = Nothing
End If
Next i
If Right(h, 2) = ", " Then
h = Left(h, Len(h) - 2)
c.Offset(, 1) = h
End If
End If
Next c
.Columns(2).AutoFit
End With
Application.ScreenUpdating = True
End Sub``````

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the FindUniques macro.

I will try this and let you know how it works.

kescco,

If your raw data worksheets do NOT contain titles in row 1, then:

Sample worksheets:

Excel 2007
AB
1DOGCOLLIE
2DOGGERMAN SHEPHERD
3DOGPOODLE
4DOG
5
2

Excel 2007
AB
1DOG
2
1

After the macro (using two arrays in memory) in worksheet 1:

Excel 2007
AB
1DOGCOLLIE, GERMAN SHEPHERD, POODLE
2
1

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
``````Sub FindUniques_V2()
' hiker95, 09/17/2014, ME806189
Dim w1 As Worksheet, w2 As Worksheet
Dim o As Variant, a As Variant
Dim i As Long, j As Long
Dim lro As Long, lra As Long, h As String
Application.ScreenUpdating = False
Set w1 = Sheets("1")
lro = w1.Cells(Rows.Count, 1).End(xlUp).Row
o = w1.Range("A1:B" & lro)
Set w2 = Sheets("2")
lra = w2.Cells(Rows.Count, 1).End(xlUp).Row
a = w2.Range("A1:B" & lra)
For i = 1 To lro Step 1
h = ""
If o(i, 1) <> "" Then
For j = 1 To lra Step 1
If a(j, 1) = o(i, 1) And a(j, 2) <> "" Then
h = h & a(j, 2) & ", "
End If
Next j
If Right(h, 2) = ", " Then h = Left(h, Len(h) - 2)
o(i, 2) = h
End If
Next i
With w1
.Range("A1:B" & lro).Value = o
.Columns(2).AutoFit
.Activate
End With
Application.ScreenUpdating = True
End Sub``````

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the FindUniques_V2 macro.

I get an error message which says:

Run-time Error 9
Subscript Out of Range

Any suggestions?

kescco,

I get an error message which says:

Run-time Error 9
Subscript Out of Range

Have you tried both macros?

It is always best to display your actual raw data worksheet(s), and, the results that you are looking for. This way we can usually find a solution on the first go.

In order to get it right this next time, we will have to see your actual raw data worksheets.

sensitive data changed

mark the workbook for sharing

Yes, I ran both macros and received errors on the ninth line of code.

Both times I received the aforementioned error.

kescco,

It is always best to display your actual raw data worksheet(s), and, the results that you are looking for. This way we can usually find a solution on the first go.

In order to get it right this next time, we will have to see your actual raw data worksheets.

sensitive data changed

mark the workbook for sharing

If you are not able to provide your workbook/spreadsheet/worksheets Excel file, then:

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.

Last edited:

Replies
4
Views
478
Replies
3
Views
173
Replies
10
Views
323
Replies
4
Views
218
Replies
7
Views
196

1,219,936
Messages
6,151,069
Members
451,006
Latest member
dhinze84

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back