我有两列,一列包含一串文本(A1
),另一列为空白(A2
)。
A1
看起来像这样:
“关于南部非洲、罗得西亚、苏伊士运河以东(包括苏联海军扩张)、阿以争端、劫机、东西方关系、北约、英国和欧洲共同体的简介”
A1
我需要在中输入中出现的国家名称A2
。例如,在上面我将输入Rhodesia, Soviet Union, Israel, United Kingdom
。
是否存在一个公式可以用来查找特定的单词然后将它们放入A2
?
答案1
您需要创建一个函数链。以下步骤在 Excel 2010 中有效,并假设您只有上述文本A1
:
- 将以下公式复制到单元格中
B1
:=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,","," "),"-"," "),"(",""),")","")
- 此公式仅考虑原始文本中包含的标点符号。为了捕获更多标点符号,您必须用另一个 SUBSTITUTE( 将此公式括起来<<当前公式>>,<<引号中的标点>>,<<根据所需结果选择“”或“”>>)。
- 将单元格命名为
B1
“SearchText”(不带引号)。您可以按如下方式执行此操作:- 按
Alt,M,M,D
打开New Name
对话框。覆盖建议的名称并按Enter
。 - 单击
Name Box
公式编辑栏左侧的,然后输入名称,并按Enter
。
- 按
D
在从单元格开始的列中输入您所在的国家/地区列表D1
。- 选择列
D
并将其命名为“国家”,如前所示。 - 在单元格中
C1
输入以下公式:=IF(IFERROR(SEARCH(CONCATENATE(Countries," "),SearchText),FALSE),Countries,"")
- 在单元格中
C2
输入以下公式:=IF(IFERROR(SEARCH(CONCATENATE(Countries," "),SearchText),FALSE),IF(C1="",Countries,CONCATENATE(C1,", ",Countries)), C1)
- 将其复制
C2
并粘贴到整个列中,结束于(假设 196 个国家/地区)单元格C196
。 - 在单元格中
A2
输入以下公式:=C196
- 您可以隐藏或分组列
B
以D
使内容更加整洁。
- 您可以隐藏或分组列
每次您在 中输入一段新文本时A1
,公式都会重新计算,并且结果将显示在 中A2
。希望这对您有所帮助。
答案2
尝试这个小的用户定义函数:
Public Function XtractNations(t As String) As String
Dim Nations(1 To 48) As String
Dim s As String
Nations(1) = "China"
Nations(2) = "Japan"
Nations(3) = "Germany"
Nations(4) = "France"
Nations(5) = "United Kingdom"
Nations(6) = "Brazil"
Nations(7) = "Russia"
Nations(8) = "Italy"
Nations(9) = "India"
Nations(10) = "Canada"
Nations(11) = "Australia"
Nations(12) = "Spain"
Nations(13) = "Mexico"
Nations(14) = "South Korea"
Nations(15) = "Indonesia"
Nations(16) = "Turkey"
Nations(17) = "Netherlands"
Nations(18) = "Saudi Arabia"
Nations(19) = "Switzerland"
Nations(20) = "Iran"
Nations(21) = "Sweden"
Nations(22) = "Norway"
Nations(23) = "Poland"
Nations(24) = "Belgium"
Nations(25) = "Argentina"
Nations(26) = "Austria"
Nations(27) = "Thailand"
Nations(28) = "South Africa"
Nations(29) = "United Arab Emirates"
Nations(30) = "Venezuela"
Nations(31) = "Colombia"
Nations(32) = "Denmark"
Nations(33) = "Malaysia"
Nations(34) = "Singapore"
Nations(35) = "Chile"
Nations(36) = "Hong Kong"
Nations(37) = "Nigeria"
Nations(38) = "Egypt"
Nations(39) = "Philippines"
Nations(40) = "Greece"
Nations(41) = "Finland"
Nations(42) = "Israel"
Nations(43) = "Pakistan"
Nations(44) = "Portugal"
Nations(45) = "Ireland"
Nations(46) = "Algeria"
Nations(47) = "Peru"
Nations(48) = "Kazakhstan"
s = ""
For i = 1 To 48
If InStr(1, t, Nations(i)) > 0 Then
s = s & ", " & Nations(i)
End If
Next i
XtractNations = Right(s, Len(s) - 1)
End Function
您可以根据需要将任意数量的国家添加到列表中。