【问题标题】:Excel2011: Vlookup and CombineExcel2011:Vlookup 和组合
【发布时间】:2017-06-22 17:46:53
【问题描述】:

在 70000+ 行的 excel 文件中组合几个函数来做我想做的事情时遇到了一些困难。非常感谢任何提示或指示或建议。

我有 2 列(价值约 70000 行)。在第 1 列中,我有客户的帐号(有重复),在第 2 列的旁边,我有要提取的数据。我还有第三列(第 3 列),它是帐号列表,但已删除重复项。我试图让我们 Vlookup 查看第三列的第一行(lookup_value),然后在(table_array)内的第 1 列中搜索该值,并返回与第 1 列值相邻的第 2 列的值。

问题,我希望 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           

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

【问题讨论】:

    标签: excel udf textjoin vba


    【解决方案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(,)))
    

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

    这是一些标准的公共模块代码,它们将使用二维数组和脚本字典收集它们。

    此子过程要求您使用工具、参考将 Microsoft Scripting Runtime 添加到 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
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多