简体   繁体   中英

Returning multiple values in a single cell that match criteria

I am trying to find a function that will help me do the following (without using VBA). So suppose i have the following data:

http://i.imgur.com/Ql2Z7yR.jpg

I need it to be the following format:

http://i.imgur.com/OkRsbKE.jpg

Basically, I need to get all the associated sku's (column F) from the parent_sku (column E) into column G comma delimited.

Excel's native worksheet formulas do not handle concatenating an unknown number of strings together and compensating for the maximum number possible can get messy. A User Defined Function¹ (aka UDF) takes advantage of VBA's ability to process loops through a large number of rows while making numerical or string comparisons and concatenations.

I had a hard time understanding your data; it seems you are arbitrarily swapping out 506 for 541468 and only implied that the sku data was to have the same replacements made by the screenshots you provided. For a more universal solution, I will just consider E13:F13 to be 506 and not 541468.

build_SKU_List UDF ¹

Function build_SKU_List(sku As Variant, parent_sku As Range, skus As Range, _
                        Optional delim As String = ", ")
    Dim str As String, rw As Long

    With skus.Parent
        Set skus = .Cells(parent_sku.Rows(1).Row, skus.Column). _
                      Resize(parent_sku.Rows.Count, skus.Columns.Count)
    End With

    For rw = Application.Match(sku, parent_sku, 0) _
          To Application.Match(sku, parent_sku) - 1
        str = str & skus(rw) & delim
    Next rw

    build_SKU_List = Left(str, Len(str) - Len(delim))
End Function

In your sample data's G2 as,

=IF(E2=F2, build_SKU_List(E2,E:E,F:F ), "")

Override the default <comma><space> delimiter if desired. Results should be similar to the following.

build_SKU_List
build_SKU_List applied to column G:G


¹ A User Defined Function (aka UDF) is placed into a standard module code sheet. Tap Alt + F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module ( Alt + I , M ). Paste the function code into the new module code sheet titled something like Book1 - Module1 (Code) . Tap Alt + Q to return to your worksheet(s).

I didn't understand your request well But you can try the next

=E7&"-Small,"&E7&"-Medium,"&E7&"-large,"&E7&"-XL,"&E7&"-2XL"

Credit: http://www.get-digital-help.com/2010/12/20/excel-udf-lookup-and-return-multiple-values-concatenated-into-one-cell/

Basically, it works like a VLOOKUP only it concatenate's every value in one cell associated with your lookup value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM