简体   繁体   English

Excel Lookup 在删除重复项时水平返回多个值

[英]Excel Lookup return multiple values horizontally while removing duplicates

I would like to do a vertical lookup for a list of lookup values and then have multiple values returned into columns for each lookup value.我想对查找值列表进行垂直查找,然后将多个值返回到每个查找值的列中。 I actually managed to do this after a long Google search, this is the code:经过长时间的谷歌搜索,我实际上设法做到了这一点,这是代码:

=INDEX(Data!$H$3:$H$70000, SMALL(IF($B3=Data!$J$3:$J$70000, ROW(Data!$J$3:$J$70000)-MIN(ROW(Data!$J$3:$J$70000))+1, ""), COLUMN(A$2)))

Now, my problem is, as you can see in the formula, my lookup range contains 70,000 rows, which means a lot of return values.现在,我的问题是,正如您在公式中看到的,我的查找范围包含 70,000 行,这意味着很多返回值。 But most of these return values are double.但大多数这些返回值都是双精度的。 This means I have to drag above formula over many columns until all lookup values (roughly 200) return #NUM..这意味着我必须将上面的公式拖到许多列上,直到所有查找值(大约 200)返回 #NUM..

Is there any possible way, I guess VBA is necessary, to return the values after duplicates have been removed?有没有什么可能的方法,我想 VBA 是必要的,在删除重复项后返回值? I'm new at VBA and I am not sure how to go about this.我是 VBA 的新手,我不知道该怎么做。 Also it takes forever to calculate having so many cells.计算有这么多细胞也需要永远。

[Edited] [编辑]

You can do what you want with a revised formula, not sure how efficient it will be with 70,000 rows, though.你可以用修改后的公式做你想做的事,但不确定 70,000 行的效率如何。

Use this formula for the first match将此公式用于第一场比赛

=IFERROR(INDEX(Data:$H3,$H70000,MATCH($B3:Data,$J3,$J70000,0)),"")

Now assuming that formula in in F5 use this formula in G5 confirmed with CTRL+SHIFT+ENTER and copied across现在假设F5中的公式在 G5 中使用此公式,用CTRL+SHIFT+ENTER并复制

=IFERROR(INDEX(Data:$H3,$H70000,MATCH(1:($B3=Data:$J3,$J70000)*ISNA(MATCH(Data!$H3:$H70000, $F5:F5 ,0)),0)),"") =IFERROR(索引(数据:$H3,$H70000,匹配(1:($B3=数据:$J3,$J70000)*ISNA(匹配(数据!$H3:$H70000, $F5:F5 ,0)) ,0)),"")

changed the bolded part depending on location of formula 1根据公式 1 的位置更改了粗体部分

This will give you a list without repeats.....and when you run out of values you get blanks rather than an error这将为您提供一个没有重复的列表.....当您用完所有值时,您会得到空白而不是错误

Not sure if you're still after a VBA answer but this should do the job - takes about 25 seconds to run on my machine - it could probably be accelerated by the guys on this forum:不确定你是否还在寻找 VBA 答案,但这应该可以完成工作——在我的机器上运行大约需要 25 秒——它可能会被这个论坛上的人加速:

Sub ReturnValues()

Dim rnSearch As Range, rnLookup As Range, rnTemp As Range Dim varArray
As Variant Dim lnIndex As Long Dim strTemp As String

Set rnSearch = Sheet1.Range("A1:A200") 'Set this to your 200 row value range
Set rnLookup = Sheet2.Range("A1:B70000") 'Set this to your lookup range (assume 2
columns)

varArray = rnLookup

For Each rnTemp In rnSearch
    For lnIndex = LBound(varArray, 1) To UBound(varArray, 1)
        strTemp = rnTemp.Value
        If varArray(lnIndex, 1) = strTemp Then
            If WorksheetFunction.CountIf(rnTemp.EntireRow, varArray(lnIndex, 2)) = 0 Then 'Check if value exists already
                Sheet1.Cells(rnTemp.Row, rnTemp.EntireRow.Columns.Count).End(xlToLeft).Offset(0, 1).Value =
varArray(lnIndex, 2)
            End If
        End If
    Next Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM