Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

excel - Excel2011: Vlookup and Combine

I'm having some difficulty combining several functions to do what I want in a 70000+ line excel file. ANY tips or pointers or advice greatly appreciated.

I have 2 columns (about 70000 lines worth). In column 1 I have account numbers for clients (there are duplicates), next to it in column 2 I have the data I want to extract. I also have a third column (column 3) which is a list of the account numbers but has been stripped of duplicates. I am trying to us Vlookup to look at line one of Column three (lookup_value) then search for that value in columns 1 within (table_array), and return the value from column 2 that is adjacent to the column 1 value.

Problem, I want Vlookup to perform this function for all 70000 rows, such that, it returns all the data that matches that particular account number provided to it with (lookup_value). THEN, I want to use the Combine function to put the string of data into a single cell using this Combine function:

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

Ultimately, next to column 3, I'd like the data to be separated by commas in a single cell, next to each account number. Below is an example of what I'm trying to do. I have the first 3 columns, but I want to convert it into the 4th column.

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           

I thought this would be a simple function or formula, but maybe I'm not using the right one(s).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function can take conditions when entered as an array formula with 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

If you do not have the newer function in your version of Excel, search this site's tag for VBA UDF and worksheet formula alternates. I've created a couple using static dict as scripting.dictionary.

Here is some standard public module code that will collect them all using a 2-D array and a scripting dictionary.

This sub procedure requires that you add Microsoft Scripting Runtime to the VBA project using Tools, References.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...