+ Reply to Thread
Results 1 to 9 of 9

Concatenate based upon criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2015
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    5

    Concatenate based upon criteria

    I did search the forum but haven't found a thread that appears to address this--if I missed it, my apologies.

    I'm using Excel 2010 and have 2 worksheets and want to lookup values from Worksheet1 and concatenate data in Worksheet2 Column D into a cell in Worksheet 1. I've been wracking my brain trying to figure this out and thought folks here must have encountered a similar situation.

    Constraints:
    I am unable to use Morefunc.
    I am not proficient in VBA.

    Does anyone have any thoughts?

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,082

    Re: Concatenate based upon criteria

    Can you give us some examples?
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: Concatenate based upon criteria

    Please attach a sample workbook.
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

  4. #4
    Registered User
    Join Date
    04-01-2015
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    5

    Re: Concatenate based upon criteria

    Many thanks for your quick replies!

    Here is the example spreadsheet. The desired state is to populate column C, Extra Territories, with concatenated values from the values on the Territory worksheet's Extra Territory column B.

    Please disregard the column/row references in the OP.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,082

    Re: Concatenate based upon criteria

    Try this (backup your data first)

    Sub k1()
    Dim lastrow1, lastrow2 As Long
    lastrow1 = Worksheets("Business").Cells(Rows.Count, "A").End(xlUp).Row
    lastrow2 = Worksheets("Territory").Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To lastrow1
    k = ""
    For j = 1 To lastrow2
    If Worksheets("Business").Cells(i, 1) = Worksheets("Territory").Cells(j, 1) Then
        k = k & Worksheets("Territory").Cells(j, 2) & ", "
    End If
    Next j
    If Len(k) <> 0 Then
    k = Left$(k, Len(k) - 2)
    End If
    Worksheets("Business").Cells(i, 3) = k
    Next i
    End Sub

  6. #6
    Registered User
    Join Date
    04-01-2015
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    5

    Re: Concatenate based upon criteria

    I should point out that the actual spreadsheets will likely grow into a few hundred rows in the Territory worksheet, and a very manageable, much smaller number on the Business worksheet. As it is, one can obviously filter as necessary with as few rows as there are on the Example.xlsx.

  7. #7
    Registered User
    Join Date
    04-01-2015
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    5

    Re: Concatenate based upon criteria

    Special K, that's beautiful! Much obliged for the code. I will need to fiddle with it a little bit--I love that it sorts the string as well!

  8. #8
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2505
    Posts
    13,766

    Re: Concatenate based upon criteria

    Quote Originally Posted by ed_han View Post

    .....Constraints:
    I am unable to use Morefunc.
    I am not proficient in VBA.

    Does anyone have any thoughts?
    You don't have to be proficient in VBA to use this User Defined Function. Copy and paste it into a module in the VBA editor. It is called Concatall. It was written by tigeravatar and shows up frequently on the Forum.

    'tigeravatar ExcelForum
    Public Function ConcatAll(ByVal varData As Variant, Optional ByVal sDelimiter As String = vbNullString, Optional ByVal bUnique As Boolean = False) As String
    'Created by TigerAvatar at www.excelforum.com, September 2012
    'Purpose is to concatenate many strings into a single string
    'Can be used with arrays, range objects, and collections
        
        Dim DataIndex As Variant    'Used to loop through arrays, range objects, and collections
        Dim strResult As String     'Used to build the result string
        
        'Test if varData is an Array, Range, or Collection
        If IsArray(varData) _
        Or TypeOf varData Is Range _
        Or TypeOf varData Is Collection Then
            
            'Found to be an, array, range object, or collection
            'Loop through each item in varData
            For Each DataIndex In varData
                'Check if the item isn't empty
                If Len(DataIndex) > 0 Then
                    'Found the item isn't empty, check if user specified bUnique as True
                    If bUnique = True Then
                        'bUnique is true, check if the item has been included in the result yet
                        If InStr(1, "||" & strResult & "||", "||" & DataIndex & "||", vbTextCompare) = 0 Then
                            'Item has not been included in the result, add item to the result
                            strResult = strResult & "||" & DataIndex
                        End If
                    Else
                        'bUnique is not true, add item to the result
                        strResult = strResult & "||" & DataIndex
                    End If
                End If
            Next DataIndex
            
            'Correct strResult to remove beginning delimiter and convert "||" to the specified sDelimiter
            strResult = Replace(Mid(strResult, 3), "||", sDelimiter)
            
        Else
            'Found not to be an array, range object, or collection
            'Simply set the result = varData
            strResult = varData
        End If
        
        'Output result
        ConcatAll = strResult
        
    End Function
    This is one way to use it. This formula must be array-entered. Array enter means the formula must be committed by simultaneously pressing and holding down Ctrl and Shift while hitting Enter.
    Formula: copy to clipboard
    =ConcatAll(IFERROR(IF(A2=Territory!$A$14:$A$33,Territory!$B$14:$B$33,""),""),", ")
    Then fill down.

    Results look like this:

    Row\Col
    A
    B
    C
    2
    Business 1
    111
    619, 318, 320, 615, 306, 321, 322, 330
    3
    Business 2
    116
    355, 345, 148, 139, 140
    4
    Business 3
    355
    116, 119, 118, 220, 230, 614, 615


    Is this what you are wanting?

    Edit formula change.
    Last edited by FlameRetired; 04-02-2015 at 11:37 AM.

  9. #9
    Registered User
    Join Date
    04-01-2015
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    5

    Re: Concatenate based upon criteria

    OK, I thought I had this figured out. Wrong.

    I can't help thinking I'm missing something really basic here but I'm just not seeing it. A little help, please?
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Concatenate cells based on criteria/threshold
    By amartin575 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-19-2015, 12:26 PM
  2. Concatenate a range based on Criteria
    By nuruedriss in forum Excel General
    Replies: 1
    Last Post: 11-06-2013, 10:32 AM
  3. Pls help, can we concatenate text based on some criteria lik SUMIFS
    By Shermin in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-22-2013, 04:31 AM
  4. Concatenate Pivot Table fields based on criteria
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2012, 12:52 PM
  5. Concatenate multiple cells based on specific criteria
    By satkadeb in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-25-2011, 08:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1