Excel2011:Vlookup和Combine

我有一些困难,结合几个function做我想要在70000 +行Excel文件。 任何提示或指针或build议非常赞赏。

我有2列(约70000行值)。 在第一列中,我有客户的帐号(有重复的),在第二列旁边有我想提取的数据。 我也有第三列(第3列),这是一个帐号列表,但已被删除重复。 我想让我们Vlookup看第三列(lookup_value)的第一行,然后在(table_array)内的列1中search该值,并返回第二列中与列1值相邻的值。

问题是,我想让Vlookup为所有70000行执行这个函数,这样它将返回所有与提供给它的特定帐号相匹配的数据(lookup_value)。 那么,我想要使用Combine函数将这个数据串放到一个使用这个Combine函数的单元格中:

Function Combine(WorkRng As Range, Optional Sign As String = ", ") As String 'Update 20130815 Dim Rng As Range Dim OutStr As String For Each Rng In WorkRng If Rng.Text <> ", " Then OutStr = OutStr & Rng.Text & Sign End If Next Combine = Left(OutStr, Len(OutStr) - 1) End Function 

最后,在第3列旁边,我希望数据在每个帐号旁边的单个单元格中用逗号分隔。 下面是我想要做的一个例子。 我有前三列,但是我想把它转换成第四列。

 Acct # Data Accounts Desired Data formating 1001 80100 1001 80100, 80250, 80255 1001 80250 1005 81000, 81222, 81235, 85213 1001 80255 1099 82250, 82323, 80100, 80150 1005 81000 1005 81222 1005 81235 1005 85213 1099 82250 1099 82323 1099 80100 1099 80105 

我认为这将是一个简单的函数或公式,但也许我没有使用正确的(S)。

input为带有CSE的数组公式时,可以使用textjoin函数。

 =TEXTJOIN(", ", TRUE, IF(A2:INDEX(A:A,MATCH(1E+99,A:A))=C2, B2:INDEX(B:B,MATCH(1E+99,A:A)), TEXT(,))) 

在这里输入图像说明

如果您的Excel版本中没有新的textjoin函数,请为VBA UDF和工作表公式替代项search此站点的textjoin标记。 我创build了一个使用static dict as scripting.dictionary

以下是一些标准的公共模块代码,它们将使用2-D数组和脚本字典来收集这些代码。

此子过程要求您使用“工具”,“引用”将Microsoft脚本运行时添加到VBA项目。

 Option Explicit Sub qwewrety() Dim delim As String, arr As Variant Dim d As Long, dict As New Scripting.dictionary delim = Chr(44) & Chr(32) With Worksheets("sheet3") arr = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "B").End(xlUp)).Value2 For d = LBound(arr, 1) To UBound(arr, 1) If dict.exists(arr(d, 1)) Then dict.Item(arr(d, 1)) = dict.Item(arr(d, 1)) & delim & arr(d, 2) Else dict.Item(arr(d, 1)) = arr(d, 2) End If Next d .Cells(2, "C").Resize(dict.Count) = Application.Transpose(dict.keys) .Cells(2, "D").Resize(dict.Count) = Application.Transpose(dict.items) End With End Sub