+ Reply to Thread
Results 1 to 5 of 5

Compare columns in 2 sheets and then return a value in another column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-25-2017
    Location
    UK
    MS-Off Ver
    Office 365 Excel Version 2202 Build 16
    Posts
    184

    Compare columns in 2 sheets and then return a value in another column

    Hi,

    I have attached my excel spreadsheet which I have been trying to develop. The problem I have is as follows:

    I have an old list or organizations. A system generates a new list of organizations however it does not produce the Owner column.

    I have an old list of organizations which I need to compare monthly to a new list of organizations to highlight differences and identify the owner in the new list organizations.

    Ignore the conditional formatting - I think I have found a formula to highlight the differences whereby a comparison is made and a value appears in the ACTION column.

    I need a formula which:

    1. Compares the org code in the new list to the org code in old list to check if they match.
    2. if the org codes in both sheets match, the OWNER column in the NEW sheet should be populated with the value from the OWNER column in the OLD sheet.
    3. if the org codes do not match no value should appear in the OWNER Column of the new sheet.

    Any help would be appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Compare columns in 2 sheets and then return a value in another column

    Try this code:

    Public Sub Pass1()
       Dim Ary As Variant
       Dim i As Long
       Dim Dest As Range
       Dim las As Long
       
       With Sheets("New")
            Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value2
            Set Dest = Range("D2")
            las = .Cells(Rows.Count, "D").End(xlUp).Row
            Range("D2:D" & las).Value = ""
            .Activate
       End With
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 1 To UBound(Ary)
                .Item(Ary(i, 1)) = Empty
            Next i
            With Sheets("old")
                Ary = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value2
            End With
            For i = 2 To UBound(Ary)
                If .Exists(Ary(i, 1)) Then .Remove Ary(i, 1)
            Next i
          Ary = .keys
        End With
        Sheets("old").Activate
        Set Dest = Dest.Resize(UBound(Ary) + 1, 1)
        Dest.Value = Application.Transpose(Ary)
        'MsgBox Join(Ary)
          
        Call pass2
        
    End Sub
    Public Sub pass2()
       Dim Ary As Variant
       Dim i, la As Long
       Dim Dest As Range
       
       With Sheets("old")
            Ary = .Range("a2", .Range("a" & Rows.Count).End(xlUp)).Value2
            Set Dest = Range("D2")
       End With
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 1 To UBound(Ary)
                .Item(Ary(i, 1)) = Empty
            Next i
            With Sheets("old")
                .Activate
                la = .Cells(Rows.Count, "D").End(xlUp).Row
                Range("D2:D" & la).Value = ""
            End With
            With Sheets("new")
                Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value2
            End With
            For i = 1 To UBound(Ary)
                If .Exists(Ary(i, 1)) Then .Remove Ary(i, 1)
            Next i
          Ary = .keys
        End With
    
    
        Set Dest = Dest.Resize(UBound(Ary) + 1, 1)
        Dest.Value = Application.Transpose(Ary)
        MsgBox Join(Ary)
       
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-25-2017
    Location
    UK
    MS-Off Ver
    Office 365 Excel Version 2202 Build 16
    Posts
    184

    Re: Compare columns in 2 sheets and then return a value in another column

    Hi,

    Thank you. I tried the code but I just get a msgbox that appears stating the number 4.

    Not sure if you have mis-understood my requirement. Sorry if comes across as confusing.

    I just need a formula that will check to see if the org codes match on both sheets and then populate the 'owner' column in the sheet NEW with whatever is populated in the owner column in sheet OLD. Thanks

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Compare columns in 2 sheets and then return a value in another column

    Here is another attempt.
    Option Explicit
    
    Sub ChangeFilter()
    Dim cel, rngw, clr, crno As Range
    Dim old, nww As Worksheet
    Dim lrO, lrN, lra, r, c As Long
    Application.ScreenUpdating = False
    Set old = ThisWorkbook.Sheets("old")
    Set nww = ThisWorkbook.Sheets("new")
    
    lrO = old.Range("A" & Rows.Count).End(xlUp).Row
    lrN = nww.Range("A" & Rows.Count).End(xlUp).Row
    
    Set crno = old.Range("A2:A" & lrO)
    Set rngw = nww.Range("A2:A" & lrN)
    
    r = 2
    c = 2
    
    For Each cel In rngw
        r = r + 1
        For Each clr In crno
            c = c + 1
            If clr = cel Then
                cel.Offset(0, 3) = "old"
                cel.Offset(0, 2) = clr.Offset(0, 2)
            End If
        Next clr
    Next cel
    
    For Each cel In crno
        r = r + 1
        For Each clr In rngw
            c = c + 1
            If clr = cel Then
                cel.Offset(0, 3) = "old"
            End If
        Next clr
    Next cel
    
    Application.ScreenUpdating = True
    
    End Sub

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,019

    Re: Compare columns in 2 sheets and then return a value in another column

    An alternative solution is with Power Query/Get and Transform found on the Data Tab of the Ribbon. A simple case of joining the two tables and looking for matches and differences.

    MCode follows:
    let
        Source = Table.NestedJoin(OLD, {"Org Code"}, NEW, {"Org Code"}, "NEW", JoinKind.FullOuter),
        #"Expanded NEW" = Table.ExpandTableColumn(Source, "NEW", {"Org Code", "Organisation Name"}, {"Org Code.1", "Organisation Name.1"}),
        #"Added Custom" = Table.AddColumn(#"Expanded NEW", "Custom", each if[Org Code]=[Org Code.1] then "Match" else if [Org Code]= null then "Add" else "Delete")
    in
        #"Added Custom"
    Review PQ
    In the attached file
    Click on any cell in the new table
    On the Data Tab, click on Queries & Connections
    In the right window, double click to open Query
    Review PQ steps

    M-code basics:
    - "let" is the start of a query
    - "in" is the closing of a query
    - each transformation step sits in between those 2 lines
    - each step line is ended by a comma, except the last one
    - "Source" is always the first step (Source Data)
    - After "in" you have the last step referenced

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    1
    Org Code Organisation Name Owner Org Code.1 Organisation Name.1 Custom
    2
    5
    Manchester John
    5
    Manchester Match
    3
    3
    Leeds John
    3
    Leeds Match
    4
    9
    Birmingham John
    9
    Birmingham Match
    5
    8
    London Tony
    8
    London Match
    6
    1
    Newcastle Tony
    1
    Newcastle Match
    7
    0
    Sheffield Tony
    0
    Sheffield Match
    8
    2
    Preston Cath
    2
    Preston Match
    9
    6
    Lawnswood Add
    10
    7
    West Park Add
    11
    4
    Liverpool Cath Delete
    Sheet: Sheet1
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compare two columns and return a value in a third column
    By Mr. H in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-09-2018, 01:38 AM
  2. [SOLVED] Compare 2 columns in different sheets and return value of third column in second sheet
    By johnjohnk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2018, 02:39 PM
  3. [SOLVED] Compare 2 columns on different sheets & return current % difference as year progresses
    By Big.Moe in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-31-2013, 03:29 AM
  4. VBA to compare data from two sheets and return value from column based on result.
    By twanbiz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2013, 11:33 AM
  5. Replies: 1
    Last Post: 10-28-2011, 03:13 AM
  6. Replies: 3
    Last Post: 01-20-2011, 11:22 AM
  7. Compare values in 2 columns, return Y/N in a third column
    By creed1101 in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 07-08-2009, 01:29 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1