根据下拉选择将文本附加到单元格值

问题描述 投票:0回答:1

我有一个公式来组合几个值:

  1. C栏中的值
  2. 如果有空格,请添加“和”
  3. 下拉列I中的选择值
  4. 根据第I列中的下拉选择分配的值(例如B1,​​C1)
  5. 将B1或C1附加到C列中的值,结果显示在M列中

M栏中的公式:

=IF(I15="Truck",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and ","And"),CHAR(32),"")),IF(I15="Bus",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")& "B1"),IF(I15="Car",CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")),"")& "C1"))

除了一小部分外,它有效;只要C列中的值为空白,默认情况下会将C1填充到M列中。 M列中的值应为空白。

有什么想法吗?

excel excel-formula excel-2010
1个回答
1
投票

真正的问题是当I15空白时。运行“评估公式”工具,您应该看到它,但是如果您破坏了代码并使用缩进/新行等格式化它:

=IF(I15="Truck",
    CLEAN(
        SUBSTITUTE(
            SUBSTITUTE(TRIM(C15),"and ","And"), 'Should this be "and" rather than "and " to match the "Bus" and "Car"?
            CHAR(32),
            ""
        )
    ),
    IF(I15="Bus",
        CLEAN(
            SUBSTITUTE(
                SUBSTITUTE(TRIM(C15),"and","And"),
                CHAR(32),
                ""
            )& "B1"
        ),
        IF(I15="Car",
            CLEAN(
                SUBSTITUTE(
                    SUBSTITUTE(TRIM(C15),"and","And"),
                    CHAR(32),
                    ""
                )
            ), 'Should this bracket be after the "C1" instead of here?
            ""
        )& "C1"
    )
)

现在,跟踪I15是否为空:I15不是“卡车”而I15不是“公共汽车”,所以我们得到:

=IF(FALSE,
    N/A,
    IF(FALSE,
        N/A,
        IF(I15="Car",
            CLEAN(
                SUBSTITUTE(
                    SUBSTITUTE(TRIM(C15),"and","And"),
                    CHAR(32),
                    ""
                )
            ), 'Should this bracket be after the "C1" instead of here?
            ""
        )& "C1"
    )
)

简化下来,你会得到:

IF(I15="Car",
    CLEAN(
        SUBSTITUTE(
            SUBSTITUTE(TRIM(C15),"and","And"),
            CHAR(32),
            ""
        )
    ),
    ""
)& "C1"

但是,I15是空白,而不是“汽车”,所以它变成IF(FALSE, N/A, "") & "C1" - 意思是,如果I15不是“公共汽车”或“卡车”,它将总是说“C1”。

此外,您可能可以将代码简化为=CLEAN(SUBSTITUTE(SUBSTITUTE(TRIM(C15),"and","And"),CHAR(32),"")) & IF(I15="Bus", "B1", IF(I15="Car", "C1", ""))

如果你可以在某处设置一个表,你可以将IF语句更改为VLOOKUP,或者如果你有一个下拉值表,你可以使用CHOOSE(MATCH(..))

© www.soinside.com 2019 - 2024. All rights reserved.