Lookup Data stored over multiple rows and combine into one cell - Macro ?

alex0182828

Board Regular
Joined
Jun 20, 2012
Messages
88
Office Version
  1. 365
Platform
  1. MacOS
<style>table { }td { padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri,sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; }</style>
I am trying to take some data stored over
many rows and combine it into one cell. I
have given an example below that's far better
than any description i can give.

Any help would be great.I don't think this
can be done with a formula but i would love
to be proven wrong !

Thanks. All

Source Data


RAN-2MM-APPLE
SAPPLE-TWST-2.5MM
RAN-2MM-APPLE SAPPLE-SQU-DC-2MM
RAN-2MM-APPLE SAPPLE-3MM
RAN-2MM-APPLE SAPPLE-2MM
SAPPLE-3MM WILD
SAPPLE-3MM RAN-2MM-APPLE
Output Required
RAN-2MM-APPLE SAPPLE-TWST-2.5MM,SAPPLE-SQU-DC-2MM,SAPPLE-3MM,SAPPLE-2MM
SAPPLE-3MM WILD,RAN-2MM-APPLE

<colgroup><col><col></colgroup><tbody>
</tbody>

<colgroup><col style="width:65pt" width="65"> </colgroup><tbody>
</tbody>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
try this UDF:

Code:
Function rngCONC(CellsRange As Range, UniqueTest As Boolean, Delimiter As Variant) As String

Dim wsTemp As Worksheet
Dim RangeCollection As Collection
Dim TargetArray As Variant, temp As Variant, finalOutput As Variant
Dim myRange As Range
Dim ItemsInColl As Integer

If IsMissing(Delimiter) Then Delimiter = ""

If UniqueTest = False Then
    For Each temp In CellsRange
    If Not temp = Empty Then
        If finalOutput = Empty Then
            finalOutput = temp
        Else
            finalOutput = finalOutput & Delimiter & temp
        End If
    End If
    Next temp
Else
    On Error Resume Next
    Set RangeCollection = New Collection
    For Each temp In CellsRange
        If Not temp = Empty Then
            RangeCollection.Add CStr(temp.Value), CStr(temp.Value)
        End If
    Next temp
    On Error GoTo 0
    
    For Each temp In RangeCollection
        If finalOutput = Empty Then
            finalOutput = temp
        Else
            finalOutput = finalOutput & Delimiter & temp
        End If
    Next temp
End If

rngCONC = finalOutput

End Function

Also you can ad arguments description to this UDF with below code (you need to run it only once)

Code:
Sub DescribeFunction_rngCONC()
   Dim FuncName As String
   Dim FuncDesc As String
   Dim Category As String
   Dim ArgDesc(1 To 3) As String

   FuncName = "rngCONC"
   FuncDesc = "Concatenate text from selected range"
   Category = 7 'Text category
   ArgDesc(1) = "is a range that need to be concatenated"
   ArgDesc(2) = "is a logical value (False or True): ""False"" to include each text from selected range, ""True"" for UNIQUE values only"
   ArgDesc(3) = "(optional) to be used between elements"
    
   Application.MacroOptions _
      Macro:=FuncName, _
      Description:=FuncDesc, _
      Category:=Category, _
      ArgumentDescriptions:=ArgDesc
End Sub
 
Upvote 0
There is no built-in concatenate if function.

You can either use the MOREFUNC library or use a user defined function (there a quite a few examples in the web)
 
Upvote 0
Excel does a fairly awful job of concatenating the way you want.
Here's a formula approach

With your sample data in A2:B7
• Put this formula in C2 and copy it down
=--(A2<>A3)

• Put this formula in D2 and copy it down
=IF(A2=A1,D1&", "&B2,B2)

• Filter the data on Col_C =1
Now you copy the visible cells and paste the values wherever you need them.

Is that something you can work with?
 
Upvote 0
Thanks for the help all

skorpionkz I popped the UDF in my sheet but i can't seem to get it to output the data as i am not sure how it works or if i need to combine it with something else i tried a bunch of different setups but i failed :( I think possibly i am not smart enough !

Ron Coderre That worked a treat !

Thanks. A
 
Upvote 0
yeah UDF i just merge range of data (doesn't to lookup) I can change it so it would do lookup as well, but Ron gave you easiest solution ;)
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top