Consulting

Results 1 to 8 of 8

Thread: Compare a column of data on one ws to another ws and append non-match data

  1. #1

    Compare a column of data on one ws to another ws and append non-match data

    I have two worksheets - LaborDetail and ECodes. In column E of LaborDetail, I want to compare that information to column B in ECodes. if there is not a match, to put that code at the bottom of the table of ECodes, column B.

    Thanks

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    without sample data it would be hard to give a good correct answer. there is a good reference on how to do this here

    http://www.mbaexcel.com/excel/how-to...t-match-match/

    It uses index/match and gives a lot of scenarios. Much better than any VLOOKUP and is more efficient. JMHO.
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello psrs0810,

    Here is a VBA solution to your problem. Add a new VBA Module to your Workbook and paste this code into it. This macro starts in row 2 of both worksheets. If your starting rows are different change them before you run the macro. The two addresses are marked.
    Sub AddUniqueData()
    
    
        Dim Dict        As Object
        Dim Key         As Variant
        Dim RngBeg      As Range
        Dim rngECodes   As Range
        Dim rngLabor    As Range
        
            Set rngLabor = Worksheets("Labor").Range("E2")        ' <<<<<< CHANGE this address
                Set RngEnd = Worksheets("Labor").Cells(Rows.Count, "E").End(xlUp)
                If RngEnd.Row > rngLabor.Row Then
                    Set rngLabor = rngLabor.Resize(RngEnd.Row - rngLabor.Row + 1, 1)
                End If
            
            Set rngECodes = Worksheets("ECodes").Range("B2")        ' <<<<<< CHANGE this address
                Set RngEnd = Worksheets("Labor").Cells(Rows.Count, "E").End(xlUp)
                If RngEnd.Row > rngECodes.Row Then
                    Set rngECodes = rngECodes.Resize(RngEnd.Row - rngECodes.Row + 1, 1)
                End If
                
            Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                ' Load ECodes into the Dictionary
                For Each Key In rngECodes
                    Key = Trim(Key)
                    If Not Dict.Exists(Key) Then
                        Dict.Add Key, True
                    End If
                Next Key
                
                ' Add missing Labor Details to ECodes
                For Each Key In rngLabor
                    Key = Trim(Key)
                    If Not Exits.Dict(Key) Then
                        Dict.Add Key, True
                    End If
                Next Key
                
            ' Output the updated ECodes
            rngECodes.Cells(1, 1).Resize(Dict.Count, 1).Value = Application.Transpose(Dict.Keys)
                
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  4. #4
    Austenr - thanks, but I am looking to do this with VBA
    Leith - I get an error on the first line.
    The worksheet is LaborDetail, which I changed the respective line, but I am not able to get it to work.

  5. #5
    I was able to get it to work - however, instead of adding any unfound codes to the bottom of the Ecode WS, it rewrote all of my codes.

  6. #6
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello psrs0810,

    If you can post a copy of your workbook it will help me eliminate errors with the macro. If you can not post it here then maybe you could a public file sharing site and post the link. As a final option, you could email it to me.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  7. #7
    How do I attach a file?
    I see under Posting Permissions, that I can post an attachment, but I do not know where/how.
    sorry - I am new to this site.
    Thanks

  8. #8
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello psrs0810,

    If you can not post it here then maybe you could a public file sharing site and post the link. As a final option, you could email it to me.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •