[英]How do I lookup multiple values from a comma separated cell and average the results in excel?
I'm trying to build a formula that can lookup multiple ISO country codes separated by comma contained in one cell (Cell A2, Image 1) with a reference to a list of country codes and education scoring (Columns F and G, Image 1). 我正在尝试构建一个公式,可以查找由一个单元格(单元格A2,图像1)中包含的逗号分隔的多个ISO国家/地区代码,并引用国家/地区代码和教育评分列表(列F和G,图像1) 。 Then return the average of the scores of all countries on cell B2.
然后返回B2上所有国家/地区的平均得分。 does anyone know if I can build a formula to handle that?
有谁知道我是否可以建立一个公式来处理它?
I didn't think you could do this with cell formula, but then I saw this post and came up with this: 我不认为你可以用细胞配方做到这一点,但后来我看到了这篇文章并想出了这个:
=AVERAGE(IF(ISNA(MATCH($F$2:$F$99, TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(A2)-LEN(SUBSTITUTE(A2,",",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(A2)-LEN(SUBSTITUTE(A2,",",""))+1)))=1),99)), 0)), "", $G$2:$G$99 ))
Try pasting into cell B2
as an array formula (Ctrl + Shift + Enter) and fill-down... And don't ask me how it works. 尝试粘贴到单元格
B2
作为数组公式(Ctrl + Shift + Enter)和填充...并且不要问我它是如何工作的。
You could try VBA: 你可以试试VBA:
Option Explicit
Sub test()
Dim i As Long
Dim strCode As String, strScore As String
Dim rngVlookup As Range
Dim Code As Variant
With ThisWorkbook.Worksheets("Sheet1")
Set rngVlookup = .Range("F2:G34")
For i = 2 To 3
strCode = ""
strScore = ""
strCode = .Range("A" & i).Value
For Each Code In Split(strCode, ",")
If strScore = "" Then
On Error Resume Next
strScore = Application.WorksheetFunction.VLookup(Trim(Code), rngVlookup, 2, False)
Else
On Error Resume Next
strScore = strScore & ", " & Application.WorksheetFunction.VLookup(Trim(Code), rngVlookup, 2, False)
End If
Next Code
With .Range("B" & i)
.Value = strScore
.NumberFormat = "0.000000"
End With
Next i
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.