+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Merge Data from rows which have a column that matches.

    Hello, I would like to create an Excel macro that compares column B on each row of a master worksheet with Column B on a temp worksheet and when a match is found, the remaining columns C-D be updated on the Master worksheet with the data from the matching row on the temp work sheet. If no match is found I would like the entire row from the temp worksheet added to the master worksheet.

    For example:

    Master
    HTML Code:
             A        B          C                D
    1      id        email      number     description
    2    501      a@xyz.com     12345        sometext1
    3    502      b@xyz.com     10101        sometext2
    4    503      c@xyz.com     98765        sometext3
    Temp
    HTML Code:
             A        B          C                D
    1      id        email      number     description
    2             g@xyz.com     98989        sometext4
    3             f@xyz.com     55555        sometext5
    4             b@xyz.com     22222        sometext6
    Resulting Worksheet:
    HTML Code:
             A        B          C                D
    1      id        email      number     description
    2    501      a@xyz.com     12345        sometext1
    3    502      b@xyz.com     22222        sometext6
    4    503      c@xyz.com     98765        sometext3
    5             g@xyz.com      98989        sometext4
    6             f@xyz.com       55555        sometext5
    As you can see, the information for b@xyz.com was updated and g@xyz.com & f@xyz.com were added.

    Preferable I would like the the Master worksheet to be updated directly, but alternatively, the results can be put in a new worksheet. The order of the rows of data does not matter.

    Thank you in advance for any help you can provide.

    Kurt
    Last edited by kurost; 03-10-2010 at 11:00 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    This macro will do what you ask, make sure the sheets are named Master and Temp:
    Code:
    removed...see below for latest version

    This macro goes in a regular module (Insert > Module), not in a sheet module.
    Last edited by JBeaucaire; 03-04-2010 at 02:14 PM. Reason: code removed, see below for latest version
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    Thanks, JBeaucaire. The first test ran successfully, but the second did not.

    My master was:
    HTML Code:
    id	name		location	age
    501	a@xyz.com	Colorado	34
    502	b@xyz.com	Broomfield	37
    503	c@xyz.com	Broomfield	10
    504	d@xyz.com	Broomfield	8
    and my temp was:
    HTML Code:
    id	name		location	age
    	d@xyz.com	Broomfield	8
    	a@xyz.com	Colorado	34
    	e@xyz.com	Aurora		5
    	f@xyz.com	Aurora		4
    	c@xyz.com	Colorado	11
    The resulting Master worksheet was:
    HTML Code:
    id	name		location	age
    501	a@xyz.com	Aurora		4
    502	b@xyz.com	Broomfield	37
    503	c@xyz.com	Colorado	11
    504	d@xyz.com	Broomfield	8
    The results I expected are:
    HTML Code:
    id	name		location	age
    501	a@xyz.com	Colorado	34
    502	b@xyz.com	Broomfield	37
    503	c@xyz.com	Colorado	11
    504	d@xyz.com	Broomfield	8
    	e@xyz.com	Aurora		5
    	f@xyz.com	Aurora		4
    It updated "a@abc.com" with the information for "f@abc.com" and didn't add e@abc.com at all. It did update c@xyz.com successfully. Again, row order doesn't matter.

    Thanks again for your help.
    Last edited by kurost; 03-04-2010 at 12:09 PM. Reason: removing email links

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    Let's see the workbook. Click GO ADVANCED and use the paperclip icon to post up your workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    I have attached Test.xls which contains the two worksheets and the macro you provided.
    Attached Files Attached Files

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    My bad, I forgot to reset the dRow after it was used each time.
    Code:
    Option Explicit
    
    Sub UpdateMaster()
    Dim dRow As Long, RNG As Range, cell As Range
    Application.ScreenUpdating = False
    
    Set RNG = Sheets("Temp").Range("B2:B" & Rows.Count).SpecialCells(xlCellTypeConstants)
    On Error Resume Next
    
    For Each cell In RNG
        With Sheets("Master")
            dRow = .Range("B:B").Find(cell, After:=.[B1], LookIn:=xlValues, LookAt:=xlPart, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
            Debug.Print cell.Address
            If dRow > 0 Then
                cell.Offset(0, 1).Resize(1, 2).Copy
                .Range("C" & dRow).PasteSpecial xlPasteValues
                dRow = 0
            Else
                cell.Resize(1, 3).Copy
                .Range("B" & .Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            End If
        End With
    Next cell
    
    Set RNG = Nothing
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    That did it.
    Thanks again for all your very prompt help!

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    After using the macro provided (which works great), instead of updating the master worksheet, it would be more effective if a new worksheet was generated that contains the both the updated rows and the rows that were added. So the new worksheet would not include any rows from the master that were not updated. Thanks again for the help.

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    This will create a new sheet with today's date and put the new list on that sheet. This work?

    Code:
    removed...see below
    Last edited by JBeaucaire; 03-09-2010 at 04:16 PM. Reason: code removed
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  11. #11
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    Sorry, this didn't work as expected, but I think that is my fault. I will post another reply witch better details as soon as a get a chance.

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    Apologies, I misread your updated request, try this:
    Code:
    code removed...see below
    Last edited by JBeaucaire; 03-09-2010 at 08:23 PM. Reason: code removed...
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  13. #13
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    This is creating an empty worksheet.

    I am using the attached spreadsheet (almost identical to the previous one). The results on the new worksheet I am expecting would be:

    HTML Code:
    id	name		location	age
    	e@xyz.com	Aurora		5
    	f@xyz.com	Aurora		4
    503	c@xyz.com	Colorado	11
    So on Temp, e@xyz.com and f@xyz.com didn't previously exist in the Master. So these would simply be copied over as is to the new worksheet. For c@xyz.com, there is a match in Master so it retains the ID and email address from the Master, but all the other columns are updated from the data from Temp.
    Attached Files Attached Files

  14. #14
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Merge Data from rows which have a column that matches.

    Hmm, try this:
    Code:
    Option Explicit
    
    Sub UpdateMaster()
    Dim dRow As Long, RNG As Range, cell As Range
    Dim wsNew As Worksheet
    Application.ScreenUpdating = False
    
    Set RNG = Sheets("Temp").Range("B2:B" & Rows.Count).SpecialCells(xlCellTypeConstants)
    
    If Not Evaluate("ISREF('New-" & Format(Date, "MMDDYY") & "'!A1)") Then _
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "New-" & Format(Date, "MMDDYY")
        
    Set wsNew = Sheets("New-" & Format(Date, "MMDDYY"))
    wsNew.Cells.Clear
    Sheets("Master").Rows(1).Copy wsNew.Range("A1")
    
    On Error Resume Next
    
    For Each cell In RNG
        dRow = Sheets("Master").Cells.Find(cell, After:=Sheets("Master").[B1], LookIn:=xlValues, LookAt:=xlPart, _
            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
        If dRow > 0 Then
            cell.Resize(1, 3).Copy
            wsNew.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            dRow = 0
        Else
            cell.Resize(1, 3).Copy
            wsNew.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
        End If
    Next cell
    
    Set RNG = Nothing
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  15. #15
    Registered User
    Join Date
    03-04-2010
    Location
    Colorado
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merge Data from rows which have a column that matches.

    The resulting new worksheet is identical to the Temp worksheet. I would like column A to retain the value (a unique ID) from the Master worksheet.

    I think another and perhaps easier approach would be to compare row B on the Temp and Master, and for any matches, update column A on the Temp worksheet with the Column A value (the ID) on the Master. I think I was making it harder than it needed to be. No need for a new worksheet to be created. Just update Temp with the matching value from Column A on the Master. My apologies for the confusion and making this more difficult than it needed to be. I greatly appreciate you assistance.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0