Excel查找2条件
Excel Lookup 2 condition
我希望你做得很好我需要支持我有数据Table见Botton
我想要这个结果
喜欢第一个匹配账户,第二个匹配选项只有3个选项我喜欢排列数字
请测试下一个代码。根据您的问题和评论,它应该 return 看起来合乎逻辑。它假定您代表所需输出的图片是错误的。它 return 在同一个 sheet 中,从“E1”开始。当然,它可能很容易适应return任何地方:
Sub TestExtractFruitsPerAccount()
Dim sh As Worksheet, lastR As Long, arr, arrIt, i As Long
Dim arrFin, j As Long, dict As Object
Set sh = ActiveSheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).Row
arr = sh.Range("A1:C" & lastR).value
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(arr)
If Not dict.Exists(arr(i, 1)) Then
dict.Add arr(i, 1), Array(arr(i, 2) & "|" & arr(i, 3))
Else
arrIt = dict(arr(i, 1)): ReDim Preserve arrIt(UBound(arrIt) + 1)
arrIt(UBound(arrIt)) = arr(i, 2) & "|" & arr(i, 3)
dict(arr(i, 1)) = arrIt
End If
Next i
ReDim arrFin(1 To dict.count, 1 To 4)
Dim strApp As String, strBan As String, strMng As String
For i = 0 To dict.count - 1
arrFin(i + 1, 1) = dict.Keys()(i)
arrIt = dict.Items()(i)
For j = 0 To UBound(arrIt)
Select Case Split(arrIt(j), "|")(1)
Case "APPLE"
If strApp = "" Then
strApp = "Pkt." & Split(arrIt(j), "|")(0)
Else
strApp = strApp & "," & Split(arrIt(j), "|")(0)
End If
Case "BANANA"
If strBan = "" Then
strBan = "Pkt." & Split(arrIt(j), "|")(0)
Else
strBan = strBan & "," & Split(arrIt(j), "|")(0)
End If
Case "MANGO"
If strMng = "" Then
strMng = "Pkt." & Split(arrIt(j), "|")(0)
Else
strMng = strMng & "," & Split(arrIt(j), "|")(0)
End If
End Select
Next j
If strApp <> "" Then arrFin(i + 1, 2) = strApp
If strBan <> "" Then arrFin(i + 1, 3) = strBan
If strMng <> "" Then arrFin(i + 1, 4) = strMng
strApp = "": strBan = "": strMng = ""
Next i
sh.Range("E1").Resize(1, 4).value = Array("Account", "APPLE", "BANANA", "MANGO")
sh.Range("E2").Resize(dict.count, 4).value = arrFin
End Sub
我把你的问题当作一个挑战,即使根据我的口味它至少很奇怪。
请测试它并发送一些反馈。这不是你需要的吗?如果没有,请尝试根据上面的代码 returns.
更好地描述您需要什么
如果有什么不清楚的,请不要犹豫,要求澄清。我可以评论代码行。我按原样发布了它,以确保您会看到它。你的问题,照原样,很可能会被删除...
我希望你做得很好我需要支持我有数据Table见Botton
我想要这个结果
喜欢第一个匹配账户,第二个匹配选项只有3个选项我喜欢排列数字
请测试下一个代码。根据您的问题和评论,它应该 return 看起来合乎逻辑。它假定您代表所需输出的图片是错误的。它 return 在同一个 sheet 中,从“E1”开始。当然,它可能很容易适应return任何地方:
Sub TestExtractFruitsPerAccount()
Dim sh As Worksheet, lastR As Long, arr, arrIt, i As Long
Dim arrFin, j As Long, dict As Object
Set sh = ActiveSheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).Row
arr = sh.Range("A1:C" & lastR).value
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(arr)
If Not dict.Exists(arr(i, 1)) Then
dict.Add arr(i, 1), Array(arr(i, 2) & "|" & arr(i, 3))
Else
arrIt = dict(arr(i, 1)): ReDim Preserve arrIt(UBound(arrIt) + 1)
arrIt(UBound(arrIt)) = arr(i, 2) & "|" & arr(i, 3)
dict(arr(i, 1)) = arrIt
End If
Next i
ReDim arrFin(1 To dict.count, 1 To 4)
Dim strApp As String, strBan As String, strMng As String
For i = 0 To dict.count - 1
arrFin(i + 1, 1) = dict.Keys()(i)
arrIt = dict.Items()(i)
For j = 0 To UBound(arrIt)
Select Case Split(arrIt(j), "|")(1)
Case "APPLE"
If strApp = "" Then
strApp = "Pkt." & Split(arrIt(j), "|")(0)
Else
strApp = strApp & "," & Split(arrIt(j), "|")(0)
End If
Case "BANANA"
If strBan = "" Then
strBan = "Pkt." & Split(arrIt(j), "|")(0)
Else
strBan = strBan & "," & Split(arrIt(j), "|")(0)
End If
Case "MANGO"
If strMng = "" Then
strMng = "Pkt." & Split(arrIt(j), "|")(0)
Else
strMng = strMng & "," & Split(arrIt(j), "|")(0)
End If
End Select
Next j
If strApp <> "" Then arrFin(i + 1, 2) = strApp
If strBan <> "" Then arrFin(i + 1, 3) = strBan
If strMng <> "" Then arrFin(i + 1, 4) = strMng
strApp = "": strBan = "": strMng = ""
Next i
sh.Range("E1").Resize(1, 4).value = Array("Account", "APPLE", "BANANA", "MANGO")
sh.Range("E2").Resize(dict.count, 4).value = arrFin
End Sub
我把你的问题当作一个挑战,即使根据我的口味它至少很奇怪。
请测试它并发送一些反馈。这不是你需要的吗?如果没有,请尝试根据上面的代码 returns.
更好地描述您需要什么如果有什么不清楚的,请不要犹豫,要求澄清。我可以评论代码行。我按原样发布了它,以确保您会看到它。你的问题,照原样,很可能会被删除...