2017-06-22 91 views
1

我有一些困难结合几个功能来做我想要的70000 +行excel文件。任何提示或指示或建议非常感谢。Excel2011:Vlookup和Combine

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

问题,我希望Vlookup对所有70000行都执行此功能,以便返回所有与提供给它的特定帐号相匹配的数据(lookup_value)。然后,我想使用的功能相结合使用该合并功能把数据的字符串转换成单细胞:

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列,我想用逗号分隔中的数据一个单元格,每个帐号旁边。下面是我想要做的一个例子。我有前3列,但我想将其转换为第4列。

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)。

回答

1

当用CSE作为数组公式输入时,函数可以采用条件。

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

enter image description here

如果您没有在您的Excel版本的新功能,搜索这个网站对VBA UDF和工作表公式交替标签。我使用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