我的直觉是使用 VBA 获取第 2 列,展开列表,在工作表 2 中搜索从第 2 列获取的值,然后在第 3 列中构建一个公式以引用在第 2 列中找到的值。第 4 列也是如此。喜欢:
Sub Stuff()
Dim numberOfReps As Integer
Dim numberOfCounties As Integer
numberOfReps = 20
numberOfCounties = 25
Dim i As Integer
Dim counties As String
Dim diagnosedEquation As String
Dim livingEquation As String
Dim rawVal As String
For i = 1 To numberOfReps
rawCounties = Sheets("Sheet1").cells(i, 2).Value
countiesArray = Split(rawCounties, ", ")
For j = 1 To numberOfCounties
rawVal = Sheets("Sheet2").cells(j, 1).Value
If (ListContains(rawVal, countiesArray)) Then
If (livingEquation = "") Then
livingEquation = "=SUM(Sheet2!C" & j
Else
livingEquation = livingEquation & "+Sheet2!C" & j
End If
If (diagnosedEquation = "") Then
diagnosedEquation = "=SUM(Sheet2!B" & j
Else
diagnosedEquation = diagnosedEquation & "+Sheet2!B" & j
End If
End If
Next j
If (Not diagnosedEquation = "") Then diagnosedEquation = diagnosedEquation & ")"
If (Not livingEquation = "") Then livingEquation = livingEquation & ")"
Sheets("Sheet1").cells(i, 3).Value = diagnosedEquation
Sheets("Sheet1").cells(i, 4).Value = livingEquation
diagnosedEquation = ""
livingEquation = ""
Next i
End Sub
Private Function ListContains(needle As String, haystack As Variant)
For Each straw In haystack
If (needle = straw) Then ListContains = True
Next straw
End Function
我已经对此进行了测试,它会总结一下。您可能需要针对您的设置对其进行一些调整,例如工作表名称(或索引)。