如何从逗号分隔的单元格中查找多个值并将结果平均为excel?

时间:2019-04-02 11:58:12

标签: excel excel-formula

我正在尝试建立一个公式,以查找国家/地区代码和教育评分列表(F和G列,单元格,单元格A2,图像1)中包含的逗号分隔的多个ISO国家/地区代码,图片1)。然后返回单元格B2上所有国家/地区的平均分数。有谁知道我是否可以建立一个公式来处理这个问题? enter image description here

2 个答案:

答案 0 :(得分:0)

我认为您无法使用单元格公式来完成此操作,但是后来我看到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 ))

尝试将其粘贴为单元​​格B2作为数组公式(按Ctrl + Shift + Enter)并填写......不要问我它是如何工作的。

答案 1 :(得分:0)

您可以尝试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