+ Reply to Thread
Results 1 to 29 of 29

Excel 2010 Macro Needed for Search, Compare & Change

  1. #1
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Question Excel 2010 Macro Needed for Search, Compare & Change

    [reposting with clearer title]

    Is there a way to create a macro that will do the following (I'm stating it as a person would do it):

    - look down column A and find any two rows that match in column A only (if other columns differ across the rows that's okay)
    Say it finds that row 10 and row 11 in column A match, then
    - look across these two matching rows to column J and determine if row 10/column J differs from row 11/column J
    - and if yes, they do differ, change row 10/column J to the contents of row 11/column J
    - lastly, after making that change, continue on down column A to find additional changes needed until the whole spreadsheet is checked

    If this is possible, please advise and maybe even provide the instructions or formula to use?

    Thanks,
    Cindi

  2. #2
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Would you mind if I sorted your sheet alphabetically by column A? I mean, is the order of your records important or can it be changed? Sorting them would make the macro run faster because it would only need to compare with the next row...
    .?*??)
    `?.???.?*??)?.?*?)
    (?.?? (?.?
    Pichingualas <---
    ??????????????????????????

    Wrap your code with CODE TAGS.
    Thank those who helped you, Don't forget to add to their REPUTATION!!! (click on the star below their post).
    Please mark your threads as [SOLVED] when they are (Thread Tools->Mark thread as Solved).

  3. #3
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    If sort is important, use a blank column and range fill with numbers. Then upon completion, you've got the info you need to restore the sort.

  4. #4
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Yeah true, but if it isn't doing that would be unnecessary... So is the order important or not Cindi?

  5. #5
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    As I understand what you're asking, I've done that type of thing before. I added a column to the sheet that I could empty and then use as a marker that I had already processed the row - marked with an "ok" if it is either the first or subsequent "copies" of the record. The cells all start out blank and are marked as evaluated. Then I do an activeCell scan (with isblank(activeCell.offset(0,10) (for instance - whatever your "ok" column is) type of comparisons to ensure that the record has not already been processed. Start the scan at range("a2").select (for example) and after each comparison, use the "activeCell.offset(1,0).select" to go to the next row. Continue the scan until you get to "if isblank(activeCell)" then quit the processing.

    For each record that you process, use a .find routine to determine if there are any more rows with that data. If there is, you can adjust cell data in either record as desired by using either activeCell.offset(0,x) and r.offset(0,x) (where R is the range variable used by the .find routine). It sounds complicated, I know, but just step through the code until you've gone through a few comparisons and adjustments and then let it go.

    Hope this helps.

  6. #6
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Actually, my column A is numbers so sorting is fine (as long as all the other row information stays with it). That is what I do now.

    If it helps, let me explain my use/reason: I own a small used books store. I export my inventory from my main database into Excel in order to upload it onto my website. Column A is the book's unique Item #; column J is the book's quantity that I have in stock. Periodically, as books are sold in my store and/or new copies come into the store, I need to update the master accordingly--only the quantity (column J mentioned above); the rest of the information across the row remains the same. So here's what I currently do: I maintain one master Excel sheet. I export from the database into Excel and highlight the whole list. Then I copy-n-paste it at the bottom of the master list, and then sort by column A. Thus I end up with one big sheet, every other line is a duplicate of the one above with one white and the duplicate a color. I need to change only the white line in column J. [As I add inventory obviously there will be rows at the bottom which are new and thus don't have a duplicate/don't need changing.] I manually do the above steps to make the changes. When I'm done with all the changes and my original master rows are now all updated, I then re-sort by color and delete the duplicate colored ones. Quite a convoluted process to do manually, and as my inventory grows it's getting phenomenally time consuming as well.

    I hope that makes sense, and thanks for helping me.


    (PS: In a perfect world I would have an inventory database that would automatically export only the listings which have changed, up or down, so I would know immediately which needed changing at all. But, frustratingly, I do not have such a thing. I aspire to that when I grow up and the budget allows.)

  7. #7
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Please Login or Register  to view this content.
    Try using this code and provide feedback if it isn't working as you want. I haven't put the color check in it which we could use if as you say the line that needs changing is white... Although you'd have to tell me what's white on it, font or background or what?
    So as I said, try using the above code, see if it works, and if not we'll see how to make it work.
    Last edited by Pichingualas; 05-04-2012 at 04:51 PM. Reason: Small error in the code

  8. #8
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Not working, I get a "Run-Time error '1001'... The sort reference is not valid"

    Does it matter that I named the macro UpdateQuantity? I changed it in your formula.

    I would attach a sample for you to play with but I can't figure out how to do that. Do you want me to email it to you?

  9. #9
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Not working, I get a "Run-Time error '1001'... The sort reference is not valid"

    Does it matter that I named the macro UpdateQuantity? I changed it in your formula.

    It doesn't matter about the color, does it? I have two colors right now, purple and blue (means different things to me, unrelated to this) -- but of course I could make them all one color for running the macro.

    I would attach a sample for you to play with but I can't figure out how to do that. Do you want me to email it to you?

  10. #10
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Sorry there was an error in the code. Here:

    Please Login or Register  to view this content.
    Try this and tell me

  11. #11
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    If you want to upload a sample file, you have to click on the "Go Advanced" button instead of "Post Quick Reply" and you will see the typical Cliplboard button which is "Attachments" at the top, or scroll down and click on "Manage Attachments" below "Additional Options". Make sure you don't have sensible information which you wouldn't want going around the web.

    Anyway, I was saying about colors in case that somehow the record you need to change was below the one you need to keep... Because that would be possible to prevent if the record that needs changing and the record you want to keep ALWAYS have different colors (for example all you need to keep are in blue and all that need updating are in purple).

  12. #12
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    No, now I get a run-time error 5 "invalid procedure call or argument"

  13. #13
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Can you point out what line its stopping at? This is weird because it's now working on my end...

    Would you please try to upload a sample file?

  14. #14
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    I think it's now attached.
    Attached Files Attached Files

  15. #15
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Here you have a sample of the code working in a file I made to test it. Check it out and tell me if it works for you:

    Compare and change test.xlsm

  16. #16
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    I'm also looking at your file.

  17. #17
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Its working on your file too on my computer:

    macrotest.xlsm

  18. #18
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Does it still not work for you? what line does it stop at? (to see that click on debug upon getting the error message)

  19. #19
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    I suppose its fixed...?

  20. #20
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Nope. Maybe I am doing something wrong on my end but I don't know what. Here are the steps I took:

    I copied your code from here, and went back to my spreadsheet
    Clicked on Macros
    Type the macro name CompareAndChange
    Click Create
    in the next window it shows
    Sub CompareAndChange ()

    End Sub
    with the cursor between the two lines
    I pasted your code where the cursor is, and Save (macro enabled workbook)
    Now I'm back in my spreadsheet
    I click Macros -- I see two entries (one selected) that both say macrotest.xlsm!Module1.CompareAndChange
    I click Run
    Get the error message "Compile error Ambiguous name detected: CompareAndChange"
    and in the box behind the error is the code
    first line says Sub CompareAndChange ()
    next Option Explicit
    space
    next Sub CompareAndChange () <---and this line is highlighted

  21. #21
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Yeah that's wrong Cindi. Why don't you do the following steps:

    Open VBA screen (ALT+F11)
    Right Click on VBAProject(Your file name)
    Insert ---> Module
    Paste the code there on the module code screen
    Close the VBA screen
    Try to run the code
    Also delete the code you had added before

    If you don't want to do that, just erase the line that's being highlighted, it shouldn't be there. And at the bottom of the code, you will also have End Sub repeated, you should erase one of them.

  22. #22
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    No, still not working. Sigh. After all this time I guess I could've done it manually, LOL.
    I give up, I don't know what else to try.

    Thank you, nonetheless. I appreciate your trying to help me with this.

  23. #23
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Try looking at the file I uploaded, that's how you should use it... Maybe you can use that file, just rename it and do what you want, it already has the macro in place...

  24. #24
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Oh, there's an idea. I'll try that. I probably won't get to work on this anymore until Monday. I'll get back to you then.

    Have a good weekend.

  25. #25
    Forum Contributor
    Join Date
    04-03-2011
    Location
    India
    MS-Off Ver
    Excel 2015
    Posts
    122

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Hi,

    there is error coming out in this below mail

    Run time errror '5' :
    Invalid Procedure call or argument

    .Header = xlNo

  26. #26
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    That line should work on Excel 2007 and 2010.

  27. #27
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    It can also be replaced with .Header = 2

  28. #28
    Registered User
    Join Date
    05-03-2012
    Location
    Plainsboro, NJ
    MS-Off Ver
    2010
    Posts
    20

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Okay, I'm finally back to this. No matter what advice from all of you I take, the macro will not work. It won't even work on Pichingualas' own spreadsheet. Is it maybe my version of Excel (I'm using 2010)? is there perhaps some setting within Excel that is not set properly? I do have macros enabled. Is it Windows Vista? I'm grabbing at straws here. It is very strange that the macro works for you but will not for me.

  29. #29
    Forum Contributor
    Join Date
    02-07-2012
    Location
    MIA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    429

    Re: Excel 2010 Macro Needed for Search, Compare & Change

    Cindi did you say your data was already sorted? If so, try using this code instead:

    Please Login or Register  to view this content.
    Try inserting that code following the steps you had done originally:

    I copied your code from here, and went back to my spreadsheet
    Clicked on Macros
    Type the macro name CompareAndChange
    Click Create
    in the next window it shows
    Sub CompareAndChange ()

    End Sub
    with the cursor between the two lines
    I pasted your code where the cursor is, and Save (macro enabled workbook)
    If your sheet is already sorted, that should work... It seems that the error comes from making the macro sort, which shouldn't be happening as it works ok on my computer but idk... maybe this way will work.

    Best of lucks and get back if you still have issues please.

    If you are going to be doing this on the same workbook you had been working so far, make sure to delete any other version of this macro that's already there please.
    Last edited by Pichingualas; 05-08-2012 at 04:41 PM.

+ 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