所以这是一张图片,以帮助澄清我的意思:
连接空单元格会留下,
,因此我想更改公式,以便如果单元格在范围J2:P2
中为空,则留空。否则CONCATENATE
电子邮件地址。
这有什么好办法?
谢谢!
答案 0 :(得分:2)
非VBA解决方案
使用
=J2 & IF(J2="","",", ") & K2 & IF(K2="","",", ") & L2 & IF(L2="","",", ") & M2 & IF(M2="","",", ") & N2 & IF(N2="","",", ") & O2
请注意,没有最后& IF(O2="","",", ")
VBA解决方案
对于使用VBA的更大范围将更简单。添加此UDF:
Function conc(r As Range) As String
Dim c As Range
For Each c In r
If c.Value <> "" Then conc = conc & c.Value & ", "
Next
If Len(conc) > 0 Then conc = Left(Trim(conc), Len(Trim(conc)) - 1)
End Function
然后像这样调用它:=conc(J2:O2)
。
要添加UDF,请按 ALT + F11 ,创建新模块并将代码粘贴到其中:
答案 1 :(得分:1)
为了避免额外的逗号,例如,只填充J2和L2,尝试将<comma><space>
连接到每个单元格值的 start ....然后使用REPLACE去除你在开始时不需要的额外的那个,即
=REPLACE(IF(J2="","",", "&J2)&IF(K2="","",", "&K2)&IF(L2="","",", "&L2)&IF(M2="","",", "&M2)&IF(N2="","",", "&N2)&IF(O2="","",", "&O2),1,2,"")