我有一个公式来组合几个值:
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列中的值应为空白。
有什么想法吗?
真正的问题是当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(..))