+ Reply to Thread
Results 1 to 11 of 11

script that will compare a specific column in sheet 1 with a column in sheet 2.

  1. #1
    Registered User
    Join Date
    03-05-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    5

    script that will compare a specific column in sheet 1 with a column in sheet 2.

    Hello,
    I am looking for a script that will compare a specific column in sheet 1 with a column in sheet 2.
    I have a report with names of people that login to certain machines and I need to compare it to a database in sheet 2.
    If the names in sheet 1 are also in sheet 2, then delete the row with that name in sheet 1. If the name is not in sheet 2 then leave it and go to the next one.
    Compare column B in sheet one with column B in sheet 2.

    I have a script (below) that almost does what I need except it doesn't always work properly. The reason my script runs 15 times is because sometimes it doesn't pick all the names but if I run it again it does... it's weird...
    Please let me know if there is a better way to do this. Thanks a lot!

    Script:
    Please Login or Register  to view this content.
    Last edited by p.f.; 03-31-2013 at 05:40 AM.

  2. #2
    Registered User
    Join Date
    03-05-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    Attached is a sample.
    Sheet 1 is the report I receive every day that has new names. I need to compare these names with sheet 2 (database) and if any of the names in sheet 1 exist in sheet 2, delete the row and continue. Whatever is left on sheet 1 after the script runs is what's not on sheet 2.
    Attached Files Attached Files

  3. #3
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    Hi and welcome to the forum. Pls take few minutes to read the forum rules, because:

    Your post does not comply with rule 3 of our forum.Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Select your code and click the [#] button at the top of the post window (if you are editing an existing post, press Go Advanced to see the [#] button).
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  4. #4
    Registered User
    Join Date
    03-05-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    All done

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    This should do. It is a bit slow as it has to delete the entire row. I would suggest just clear the single cell, instead of deleting and this will speed up the code.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-05-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    It's working but it's only doing half at a time. I pasted 30000 lines and after running the macro I have 15000 left. I run it again and it does half again...
    Any thoughts?
    Thanks.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    I do not understand by "half". On the given sample does the code matches both sheets and delete the match from sheet1, if no what is missing?

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    How about?
    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    I have got the same result by running a different code.

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    Quote Originally Posted by p.f. View Post
    It's working but it's only doing half at a time.
    It is a well-known issue when you delete row(s).
    The loop needs to be backward from the bottom, otherwise it only deletes one line when consecutive lines need to be deleted.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    Jindon,
    Thanks!
    I completely forgotten it.
    Change this line of code # 5

    Please Login or Register  to view this content.
    INTO
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-05-2013
    Location
    toronto
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: script that will compare a specific column in sheet 1 with a column in sheet 2.

    This is brilliant! Thank you all very much! All is working.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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