我有一个电子表格,其中有一列以数字形式列出参考文献,例如以下形式“1, 2, 6”。我还有一个单独的表格,将数字与实际参考文献关联起来:
有没有办法将此编号列表转换为相应的文本列表?例如,“1, 2, 6”将被翻译为“包装说明书/制造商,Trissel 第 18 版,USP 797”?
谢谢!
答案1
更新:添加 Vba
如果您不喜欢下面的关键假设#1,您可以尝试这段代码。
Function lookupStringCodes(lookupValue As Range, lookupRange As Range, lookupResult As Range)
Dim commaSepVals() As String
commaSepVals = Split(CStr(lookupValue.Value), ",")
Dim output As String
output = vbNullString
For i = LBound(commaSepVals) To UBound(commaSepVals)
commaSepVals(i) = Replace(commaSepVals(i), " ", "")
Next
Dim sep As String
sep = ", "
For j = LBound(commaSepVals) To UBound(commaSepVals)
output = output & _
Application.WorksheetFunction.Index(lookupResult, _
Application.WorksheetFunction.Match(CStr(commaSepVals(j)), lookupRange, 0))
If j < UBound(commaSepVals) Then
output = output & sep
End If
Next
lookupStringCodes = output
End Function
你可以像这样调用:
原始答案:工作表函数
关键假设:
- 就像您的示例一样,您只有 3 个整数,范围从 1...n(如果情况并非如此,并且您可以拥有超过 3 个整数,那么使用 VBA 解决这个问题会容易得多,除非您想将此文本拆分为列)
- 您希望在一个公式中完成此操作,而不是使用文本到列
请尝试以下操作:
=VLOOKUP(LEFT(A2,(FIND(",",A2,1)-1)), G$2:H$11, 2, FALSE) & ", " & VLOOKUP(LEFT(TRIM(MID(SUBSTITUTE(A2," ",REPT(" ", 100)),100,100)), LEN(TRIM(MID(SUBSTITUTE(A2," ",REPT(" ", 100)),100,100)))-1), G$2:H$11, 2, FALSE) & ", "& VLOOKUP(TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",LEN(A2))),LEN(A2))), G$2:H$11, 2, FALSE)
格式化以便于阅读:
=VLOOKUP(LEFT(A2,(FIND(",",A2,1)-1)), G$2:H$11, 2, FALSE) & ", " &
VLOOKUP(LEFT(TRIM(MID(SUBSTITUTE(A2," ",REPT(" ", 100)),100,100)),
LEN(TRIM(MID(SUBSTITUTE(A2," ",REPT(" ", 100)),100,100)))-1), G$2:H$11, 2, FALSE) & ", "&
VLOOKUP(TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",LEN(A2))),LEN(A2))), G$2:H$11, 2, FALSE)
基本上:取出第一个逗号左侧的文本,在查找表中查找。然后,取出第一个空格和第二个逗号之间的文本,在查找表中查找。最后,取出最后一个空格右侧的文本,在查找表中查找。