In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

huangapple go评论72阅读模式
英文:

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

问题

以下是已翻译的内容:

我正在寻找一个Excel公式或VBA解决方案,根据Table1中的数据和Table2中列N的输入,在Table2的Party列生成结果。Table2列N中的输入是与Table1数据行的顺序相对应的数字。Table2中的Party应该返回Table1中的ShortName,但如果ShortName为空,则Party应返回Name。谢谢你的帮助。

英文:

I am looking for an Excel formula or VBA solution that generates the result in Table2 column Party based on the data in Table1 and the inputs in Table2 column N. The inputs in column N of Table2 are numbers corresponding to the order of the rows of data in Table1. Party in Table2 should return ShortName from Table, but if ShortName is blank, then Party in Table2 should return Name.
Thank you for your help.

Table1

Name ShortName
Jon Doe Doe
Robert Smith
Susan Miller SM
Donald Duck
Micky Mouse The Mouse
Kog Enterprises, Inc. Kog
Mechanical, Inc.

Table2

N Party
1,2,3 Doe,Robert Smith,SM
4 Donald Duck
3,5,6 SM,The Mouse,Kog
2,3,5,6,7 Robert Smith,SM,The Mouse,Kog,Mechanical, Inc.

The formula below works, but only for ShortName; it does not check ShortName and, if blank, default to Name:

=TEXTJOIN(",",TRUE,
INDEX(FILTER(Table1[ShortName],Table1[ShortName]<>""),
MATCH(NUMBERVALUE(TEXTSPLIT([@N2],,",",TRUE,1)),
ROW(Table1[ShortName])-ROW(Table1[[#Headers],[ShortName]]),
0)))

The formula below correctly switches between Name and ShortName, but cannot handle multiple comma-separated values in Table2 column N:

IF(INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[[Name]:[ShortName]],Table1[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[Name],Table1[Name])<>""),SUBSTITUTE(LOWER([@N2]),"-",""),3)=0,INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[[Name]:[ShortName]],Table1[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[Name],Table1[Name])<>""),SUBSTITUTE(LOWER([@N2]),"-",""),1),INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[[Name]:[ShortName]],Table1[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([@N2],1))="-",1,2),Table3[Name],Table1[Name])<>""),SUBSTITUTE(LOWER([@N2]),"-",""),3))

The formula above also has other features not discussed here to help keep this post as simple as possible.

答案1

得分: 2

匹配数据按索引

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

  • Party 列的第1行中的公式:
=IFERROR(TEXTJOIN(", ",,INDEX(IF(
    LEN(Table2[ShortName])>0,Table2[ShortName],Table2[Name]),
        TEXTSPLIT([@N],","))),"")

  • Excel(结构化)表不支持自动填充公式。在输入公式之前,请清除 Party 列的内容,整个列将被填充。

英文:

Match Data by Indexes

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

  • The formula in the 1st row of the Party column:
=IFERROR(TEXTJOIN(", ",,INDEX(IF(
    LEN(Table2[ShortName])>0,Table2[ShortName],Table2[Name]),
        TEXTSPLIT([@N],","))),"")
  • Excel (structured) tables do not support spilling formulas. Clear the contents of the Party column before entering the formula and the whole column will be populated.

答案2

得分: 2

以下是翻译好的内容:

另一种选择是使用ARRAYTOTEXT()XLOOKUP()

• 在单元格E2中使用的公式

=LET(
    α, Table1[ShortName],
    σ, Table1[Name],
    ARRAYTOTEXT(
        XLOOKUP(
            TEXTSPLIT(
                [@N],
                ",",
                """
            ) * 1,
            ROW(α) - 1,
            IF(α = """, σ, α)
        )
    )
)

注意: 由于您在帖子中提到了Table1Table2,因此溢出数组公式不适用于结构引用,因此上述公式需要向下填充,或者由于表结构的行为,它将自动向下填充。

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

英文:

Another alternative using ARRAYTOTEXT() & XLOOKUP()

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?


• Formula used in cell E2

=LET(
    α, Table1[ShortName],
    σ, Table1[Name],
    ARRAYTOTEXT(
        XLOOKUP(
            TEXTSPLIT(
                [@N],
                ,
                ","
            ) * 1,
            ROW(α) - 1,
            IF(α = "", σ, α)
        )
    )
)

Note: Since you have mentioned in your post that you have Table1 & Table2 hence spill array formulas won't work with Structured References, therefore the above formula needs to be filled down or it will automatically fill down because of Table Structured behaviors.


答案3

得分: 1

以下是翻译好的部分:

假设没有根据您的标签进行版本约束,以下似乎可以生成您所描述的内容:

F2: =TEXTJOIN(
    ", ",
    TRUE,
    LET(
        r, --TEXTSPLIT(E2, ","),
        f, CHOOSEROWS(Table18, r),
        IF(TAKE(f, , -1) = "", TAKE(f, , 1), TAKE(f, , -1))
    )
)

并向下填充

或者作为仅在F2中输入的单个公式:

=BYROW(
    E2:E5,
    LAMBDA(arr,
        TEXTJOIN(
            ", ",
            TRUE,
            LET(
                r, --TEXTSPLIT(arr, ","),
                f, CHOOSEROWS(Table18, r),
                IF(TAKE(f, , -1) = "", TAKE(f, , 1), TAKE(f, , -1))
            )
        )
    )
)

您还可以通过将输出设置为“表格”来避免填充。通过在“Party”列的第一行输入公式,公式将自动向下填充。

英文:

Assuming no version constraints as per your tags, the following seems to produce what you describe:

F2: =TEXTJOIN(
    ", ",
    TRUE,
    LET(
        r, --TEXTSPLIT(E2, ","),
        f, CHOOSEROWS(Table18, r),
        IF(TAKE(f, , -1) = "", TAKE(f, , 1), TAKE(f, , -1))
    )
)

and fill down

Or as a single formula entered only in F2:

=BYROW(
    E2:E5,
    LAMBDA(arr,
        TEXTJOIN(
            ", ",
            TRUE,
            LET(
                r, --TEXTSPLIT(arr, ","),
                f, CHOOSEROWS(Table18, r),
                IF(TAKE(f, , -1) = "", TAKE(f, , 1), TAKE(f, , -1))
            )
        )
    )
)

You can also avoid filling down by setting your output in a Table. By entering the formula in the first row of the Party column, the formula will fill down automagically.

In Excel using VBA or formula, how to generate in single cell a comma-separated list based on complex criteria?

huangapple
  • 本文由 发表于 2023年6月4日 23:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定