+ Reply to Thread
Results 1 to 19 of 19

How to create a Macro that looks for matching rows of data on 2 different worksheets

  1. #1
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Post How to create a Macro that looks for matching rows of data on 2 different worksheets

    I am new to programming and not sure how to write a Macro, but could use it for work. I have 2 worksheets with several hundred rows of data. I want to write a program that can check to see if both worksheets contain the same data and if possible, have anything that is different appear in a seperate tab. This would avoid me searching through thousands of lines one by one. Is this feasable? Thank you for your help.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    This is possible. Please attach 2 sample files and i can help you out.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    I listed the 2 sample data sources in 2 tabs. There are more columns but I deleted some out. The columns highlighted in orange are the criteria I want to search by. Ideally, I want to be able to copy/paste data in 2 tabs, then have the macro automatch and pull out what doesnt match and list each item in the same format as the data. Thank you so much! sample data.xlsx

    Apologies, in tab Data 2, the wrong column is highlighted. Please use column L, execution quantity, NOT column K.
    Last edited by NYC4LIFE; 12-21-2011 at 11:51 AM. Reason: error

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Use this code. It will create a "Temp" sheet where it will merge the data from both tabs and then delete the matches. What will remain is the un-matched data based on the fields you provided.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    ok. wow! I'll try it now. What about those columns I deleted. If the original data has more columns then the sample, will it make a difference since I just want to specifically compare those 3 criteria I gave you?

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Once you use the macro with the original data, if the columns M, R & V are going to change (i mean the cell references will change) then the macro will get affected.
    You can provide me the following -
    1. The last column in your data
    2. The corresponding columns for M, R & V (i have inserted 1 column at the beginning so it should be L, Q & U) in your original file.

    Will the number of columns keep changing each time you run the macro or will they be static?

  7. #7
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hi Arlette,

    The number of columns will be static at 37 for each data tab. They come from 2 sources thus the need to compare them.

    1)Last column will be AK
    2)The corresponding columns for M, R & V are O, T, & AD in my original.

    Also, how can I tell if the items that are created are rows missing from Data 1, or Data 2? Is there a way to distinguish?

    Thank You !!!

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    I have made the necessary changes to the code as per the column references you gave me.

    If you check the "Temp" sheet, you will see column A contains the header Sheet with values of 1 or 2. This will tell you from where the data was pulled out. So if the row has value 1, it means i took it from the 1st sheet.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hi Arlette,

    Thank you so much!! But I'm having a problem still. I pasted the code, then created a filename "datacompare" and saved it. When I go into the sheet with my 2 tabs of data, I run the macro but it doesnt move and doesnt give an error message either. Any ideas?

    Option Explicit
    Dim lrow As Long
    Dim tlrow As Long
    Dim lcol As Long
    Dim i As Long

    Sub compare_rows()

    Worksheets.Add(After:=Worksheets(2)).Name = "Temp"

    lrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
    Worksheets(1).Rows("1:" & lrow).Copy Worksheets("Temp").Range("A" & Rows.Count).End(xlUp)

    With Worksheets("Temp")
    .Columns("A:A").Insert shift:=xlToRight
    .Range("A1").Value = "Sheet"
    .Range("A1").Font.Bold = True
    .Range("A2:A" & lrow).Value = "1"
    End With

    lrow = Worksheets(2).Range("A" & Rows.Count).End(xlUp).Row
    Worksheets(2).Range("A2:AK" & lrow).Copy Worksheets("Temp").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)

    With Worksheets("Temp")
    tlrow = .Range("A" & Rows.Count).End(xlUp).Row
    lrow = .Range("B" & Rows.Count).End(xlUp).Row
    .Range("A" & tlrow + 1 & ":A" & lrow).Value = "2"
    End With

    Cells.EntireColumn.AutoFit

    lrow = Worksheets("Temp").Range("A" & Rows.Count).End(xlUp).Row

    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Add Key:=Range("O:O"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Add Key:=Range("T:T"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Temp").Sort.SortFields.Add Key:=Range("AD:AD"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Temp").Sort
    .SetRange Range("A:AK")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    With Worksheets("Temp")
    For i = 2 To lrow
    If .Range("A" & i).Value = "1" And .Range("A" & i + 1).Value = "2" Then
    If .Range("O" & i).Value = .Range("O" & i + 1).Value Then
    If .Range("T" & i).Value = .Range("T" & i + 1).Value Then
    If .Range("AD" & i).Value = .Range("AD" & i + 1).Value Then
    .Rows(i & ":" & i + 1).Delete
    lrow = lrow - 2
    i = i - 2
    End If
    End If
    End If
    End If
    Next i
    End With

    End Sub

    Sub datacompare()

    End Sub

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    When you post code in this forum, please use code tags.

    The datacompare filename (actually its the macro name) shows at the end of this code and there is nothing in between datacompare() and End Sub. You dont need to add a new name, i have already given it a name - compare_rows.

    All you need to do is...press Alt+F11. On the left hand side, you will see microsoft excel objects. Right click on it and click on insert -> module. Put whatever code i gave you. And then run the compare_rows macro. If you want to change it, just change it where i put compare_rows.

  11. #11
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Ok sorry, didnt know about the code tags.

    Great! I got it working. Thanks so much! I have one more question for you too :-) I want to compare another set of data that has the same criteria as above, Columns (O,T,AD), but this new data is formatted differently and only has 20 columns in total. The corresponding columns are H, G, E respectively. The First column is A and the last column is N. Could you possibly create a new file with this adjustment? I thank you kindly for your time.

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Is it H, G & E after inserting the column for sheet?

  13. #13
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    What do you mean by after inserting the column for sheet? I'm confused lol The criteria that is in O, T, & AD comes up in H, G, & E in the new format data. So I need to compare these columns with each other.

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Use this code -
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hi Arlette,

    I apologize but am a bit confused as to how to save this as a new macro to run. Please forgive my novice experience. How do I get a new blank sheet in the VBA screen? Thanks

  16. #16
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    You do not need a blank VBA screen. I will alter the first few lines of the code, so that you can just copy paste it after the "End Sub" of the previous code.
    Use this altered code -
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hi Arlette, Sorry for late reply. I pasted the alternate code below the original code as mentioned, however, the macro does not run properly. It gives me both sets of data on the temp tab. I'm sure I'm not pasting it or running it properly. Any ideas? Maybe can you create a new file name for the alternate code and I can save it as a new macro? I apologies for taking your time like this.

    Best
    Thomas

  18. #18
    Registered User
    Join Date
    12-20-2011
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hi Arlette, I'm trying to run the code from step #14 but get an error message of can not execute in break mode, and the line "Sub compare_rows()" is highlighted. What am I doing wrong? Thank you so much.

  19. #19
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: How to create a Macro that looks for matching rows of data on 2 different workshe

    Hey sorry for the delay ...i was out on vacation.

    I didnt realise you were running both the macros in the same file (if i am right as per your above posts). I can do one thing - change the Temp to Temp1 or NewTemp so that the data from the 2nd macro will appear there.

    You said it gives you both sets of data. Ideally, you will get all the data that doesnt match. So it could be data from sheet 1 or sheet 2 or even both.

    Regarding your last post, i guess you were trying to run 1 macro when the other macro was just paused (and not stopped) so it gave you that error. Just close the file and open again and then run. I am altering the code in post 14 (which was also altered in post 16) so you can run both macros independently without messing up the data sets.
    Please Login or Register  to view this content.

+ Reply to Thread

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.6.0 RC 1