+ Reply to Thread
Results 1 to 4 of 4

Compare 2 columns in different sheets and return value of third column in second sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2018
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    5

    Compare 2 columns in different sheets and return value of third column in second sheet

    Hi all,

    I am trying to do the following:

    In the same workbook, I have names in column B in sheet 1 and names in column B in sheet 2. There are names in sheet 2 that are not in sheet 1.
    I would like a macro that for every name in sheet 2 will find the same name in sheet 1 and when found will copy the data from column F of the same row of sheet 1 in column C of sheet 2.
    If the name in sheet 2 is not found in sheet 1, then report 0.
    All the names in sheet 1 and sheet 2 are in the exact same format (IE_Last Name_First Name).

    I am enclosing an example.

    I tried to find a solutions on the forums but couldn't find any that suit my needs. Of course I know I could use the match index formula but I really need this in VBA.

    Thanks in advance for your help!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Compare 2 columns in different sheets and return value of third column in second sheet

    See next code
    Option Explicit
    
    Sub Treat()
    Const OrgWsName As String = "Sheet1"
    Const DstWsName As String = "Sheet2"
    Const OrgCol As String = "F"
    Const DstCol As String = "C"
    Const WkCol As String = "B"
    Const FR As Integer = 2
    Dim Rg As Range
    Dim DataDic   As Object
    Set DataDic = CreateObject("Scripting.Dictionary")
    
        With Sheets(OrgWsName)
            For Each Rg In Range(.Cells(FR, WkCol), .Cells(Rows.Count, WkCol).End(3))
                DataDic.Item(Rg.Value) = .Cells(Rg.Row, OrgCol).Value
            Next Rg
        End With
        With Sheets(DstWsName)
            For Each Rg In Range(.Cells(FR, WkCol), .Cells(Rows.Count, WkCol).End(3))
                .Cells(Rg.Row, DstCol) = 0
                If (DataDic.exists(Rg.Value)) Then _
                    .Cells(Rg.Row, DstCol) = DataDic.Item(Rg.Value)
            Next Rg
        End With
        MsgBox (" Job Done")
    End Sub
    Last edited by PCI; 06-06-2018 at 02:20 PM.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    05-15-2018
    Location
    Dublin, Ireland
    MS-Off Ver
    2016
    Posts
    5

    Re: Compare 2 columns in different sheets and return value of third column in second sheet

    Wow it's perfectly working!!! Thank you so much for your quick response!!

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Compare 2 columns in different sheets and return value of third column in second sheet

    You are welcome, not too difficult and good explanation with sample ...!
    Keep in mind for the Rep ...!

+ 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. Need VBA to compare value of columns in different sheet and return value
    By Richavlaues in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-20-2016, 03:28 PM
  2. [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
  3. 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
  4. Replies: 2
    Last Post: 11-13-2012, 02:56 PM
  5. Replies: 1
    Last Post: 10-28-2011, 03:13 AM
  6. Replies: 3
    Last Post: 01-20-2011, 11:22 AM
  7. Compare fields in two sheets ... return associated data to third sheet
    By delirium in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2010, 01:42 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