如果一个范围内的单元格空白…然后空白…否则CONCATENATE

所以这里有一张照片来帮助我澄清我的意思:

连接空单元格留下a ,所以我想更改公式,以便如果单元格在范围J2:P2空白,则留空。 否则请CONCATENATE电子邮件地址。

这是什么方法?

谢谢! 在这里输入图像说明

非VBA解决scheme

使用

 =J2 & IF(J2="","",", ") & K2 & IF(K2="","",", ") & L2 & IF(L2="","",", ") & M2 & IF(M2="","",", ") & N2 & IF(N2="","",", ") & O2 

请注意,没有最后& IF(O2="","",", ")

VBA解决scheme

对于更大的范围使用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 ,创build新模块并粘贴代码:

在这里输入图像说明

在这里输入图像说明

例如,为了避免额外的逗号,例如只填充J2和L2,请尝试将<comma><space>到每个单元格值的起始位置….然后使用REPLACE删除不需要的多余<comma><space>开始,即

=REPLACE(IF(J2="","",", "&J2)&IF(K2="","",", "&K2)&IF(L2="","",", "&L2)&IF(M2="","",", "&M2)&IF(N2="","",", "&N2)&IF(O2="","",", "&O2),1,2,"")