简体   繁体   English

检查文本或#是否在单元格中,如果并且仅存在,则添加该单元格和静态文本

[英]Check if text or # is in a cell, if and only it exists, add that cell and static text

Ok, so I'm doing mail merges for html email templates. 好的,所以我正在为HTML电子邮件模板进行邮件合并。 I have these things so close to being 100% but I just need to do the following: 我拥有的这些东西已经接近100%,但我只需要执行以下操作:

I need to insert editors and their affiliation like this: John doe (USA), Jane doe (Africa) and Frank doe (Canada) 我需要像这样插入编辑器及其隶属关系:John doe(美国),Jane doe(非洲)和Frank doe(加拿大)

The first and last names are in column A, C, and E. The affiliations are in B, D, and F. 名字和姓氏在A,C和E列中。从属关系在B,D和F中。

How can I set myself up in Excel to add the parenthesis IF there is a second or third editor. 如果有第二个或第三个编辑器,我如何在Excel中进行设置以添加括号。 And how can I make the "and" be inserted when there is only two or three editors where it needs to go? 当只有两个或三个编辑器需要插入时,如何插入“和”呢?

I will post the formula here and explain a simple method for how I work though these as it might help you in the future. 我将在此处发布公式,并解释一种简单的方法来解决我的工作,因为这些方法将来可能会对您有所帮助。

For these replicating type formula where it is just going to check the same thing again and again I find it easiest to get it working for the first couple of components then applying that logic. 对于这些可重复使用的类型公式,将一次又一次地检查同一件事,我发现最简单的方法是使它适用于前几个组件,然后应用该逻辑。

So for the first name I make the assumption there will always be at least one name there and do the formatting formula in a cell. 因此,对于第一个名称,我假设那里将始终至少有一个名称,并在单元格中执行格式化公式。

=S2 & " (" &AB2&")"

Then I do the conditional formatting of the second name in the cell to the right of it (in this case). 然后,在其右边的单元格中对第二个名称进行条件格式化(在这种情况下)。 This time I test to see if both the name and affiliation are populated and if they are the name is displayed and formatted else nothing is displayed. 这次,我测试是否同时填充了名称和从属关系,以及是否显示了名称和格式,否则未显示任何内容。

=IF(AND(T2>"",AC2>""),T2&" ("&AC2&")","")

This formula can then be copied across so it applies to all the names/affiliations columns. 然后可以复制该公式,以便将其应用于所有名称/从属列。

Next I move on to the comma/and test, this just checks if there are one or two additional items and if there are two display a comma but if only one display 'and'. 接下来,我继续进行逗号/和测试,这只是检查是否有一个或两个其他项目,并且是否有两个显示逗号,但是是否只有一个显示“和”。

=IF(AND(T2>"",AC2>"",U2>"",AD2>""),", "," and "))

This is again copied across to cover all the columns required. 再次将其复制以覆盖所需的所有列。 The only difference is the last column where it is either empty or 'and' depending if it is populated. 唯一的区别是最后一列为空或为“ and”(取决于是否填充)。

All you have to do to get everything going is, basically, concatenate the formula together via copy and paste into a single cell or by chaining cell references. 要使一切顺利,您要做的就是将公式通过复制粘贴到单个单元格中或通过链接单元格引用而串联在一起。

This isn't particularly pretty or efficient but it works, is simple once you get the hang of it and doesn't require VBA. 这并不是特别漂亮或有效,但它确实有效,一旦掌握了它就很简单,不需要VBA。

Complete formula. 完整的公式。

=S2 & " (" &AB2&")"&IF(OR(T2="",AC2=""),"",IF(AND(T2>"",AC2>"",U2>"",AD2>""),", "," and "))&IF(AND(T2>"",AC2>""),T2&" ("&AC2&")","")&IF(OR(U2="",AD2=""),"",IF(AND(U2>"",AD2>"",V2>"",AE2>""),", "," and "))&IF(AND(U2>"",AD2>""),U2&" ("&AD2&")","")&IF(OR(V2="",AE2=""),"",IF(AND(V2>"",AE2>"",W2>"",AF2>""),", "," and "))&IF(AND(V2>"",AE2>""),V2&" ("&AE2&")","")&IF(OR(W2="",AF2=""),"",IF(AND(W2>"",AF2>"",X2>"",AG2>""),", "," and "))&IF(AND(W2>"",AF2>""),W2&" ("&AF2&")","")&IF(OR(X2="",AG2=""),"",IF(AND(X2>"",AG2>"",Y2>"",AH2>""),", "," and "))&IF(AND(X2>"",AG2>""),X2&" ("&AG2&")","")&IF(OR(Y2="",AH2=""),"",IF(AND(Y2>"",AH2>"",Z2>"",AI2>""),", "," and "))&IF(AND(Y2>"",AH2>""),Y2&" ("&AH2&")","")&IF(OR(Z2="",AI2=""),"",IF(AND(Z2>"",AI2>"",AA2>"",AJ2>""),", "," and "))&IF(AND(Z2>"",AI2>""),Z2&" ("&AI2&")","")&IF(OR(AA2="",AJ2=""),""," and ")&IF(AND(AA2>"",AJ2>""),AA2&" ("&AJ2&")","")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM