+ Reply to Thread
Results 1 to 7 of 7

Compare and Copy Code Trouble

  1. #1
    Registered User
    Join Date
    07-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Compare and Copy Code Trouble

    Hello,
    I am having trouble figuring out how to fix this code and am hoping someone here can help me out. I have the code below to compare sh1 to sh2. The code works fine if my sheet has single rows that meet the criteria. If there is multiple rows that meet the criteria, it will skip over them. I'm not getting any kind of error. The code runs all the way through. It just skips the rows with multiple criteria. Below is the code and parts of each spreadsheet to look at. As you can see in the sample spreadsheets, the notes in red transfer from sh2 to sh1 just fine except for the rows that have the same date more than once in column A. It should insert a row after the last row of the multiple date rows also and insert the note from sh2. I think it is in the part of the code I made red but I do not know how to fix it. Any help would be much appreciated.


    Please Login or Register  to view this content.
    sh1.xlsx

    sh2.xlsx

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Compare and Copy Code Trouble

    You are not doing the find properly, you need to find the next one,

    so would be something like

    do until sh2.Range("E:E").Find(c.Value, , xlValues) is nothing

    loop

    Then loop through starting your search at the previous find

    range("H:h").Find("NATHAN",fAdr.cells(1,1),xlvalues,XlLookAt.xlWhole,xlnext)

    I am not in excel at the minute, so cant test.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Compare and Copy Code Trouble

    fLoc is a range and needs to be treated properly.

    Please Login or Register  to view this content.
    Also change this:

    Please Login or Register  to view this content.
    Last edited by stnkynts; 06-18-2014 at 11:04 AM.

  4. #4
    Registered User
    Join Date
    07-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Compare and Copy Code Trouble

    Quote Originally Posted by stnkynts View Post
    fLoc is a range and needs to be treated properly.

    Please Login or Register  to view this content.
    Also change this:

    Please Login or Register  to view this content.
    I changed the code per your suggestions and am still getting the same result as before. It is skipping any rows that have the date listed more than once

    New code:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Compare and Copy Code Trouble

    Quote Originally Posted by nathansav View Post
    You are not doing the find properly, you need to find the next one,

    so would be something like

    do until sh2.Range("E:E").Find(c.Value, , xlValues) is nothing

    loop

    Then loop through starting your search at the previous find

    range("H:h").Find("NATHAN",fAdr.cells(1,1),xlvalues,XlLookAt.xlWhole,xlnext)

    I am not in excel at the minute, so cant test.
    I'm sorry Nathan but I'm not understanding what you suggested I try. At least not yet. I'm still looking

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Compare and Copy Code Trouble

    Please Login or Register  to view this content.
    This line is probably causing you trouble. When looping through a range it is usually best to go from the bottom up if you are going to be adding or deleting rows; otherwise those added or deleted rows will be adversely integrated into the loop.

    The insert in conjuction with the merging of the range is probably hangining you up; VBA and merged cells do not work well together. At this point I don't know what to say other than you would probalby need to submit your workbook and have you problem evaluated rather than your solution. At this point I have no interest in doing that for you but others on the forum might.

  7. #7
    Registered User
    Join Date
    07-05-2012
    Location
    Texas
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Compare and Copy Code Trouble

    Quote Originally Posted by stnkynts View Post
    Please Login or Register  to view this content.
    This line is probably causing you trouble. When looping through a range it is usually best to go from the bottom up if you are going to be adding or deleting rows; otherwise those added or deleted rows will be adversely integrated into the loop.



    The insert in conjuction with the merging of the range is probably hangining you up; VBA and merged cells do not work well together. At this point I don't know what to say other than you would probalby need to submit your workbook and have you problem evaluated rather than your solution. At this point I have no interest in doing that for you but others on the forum might.
    I have left the merge line out and it does the same thing.

    If you don't mind, how would you write the line you believe is causing the trouble? I have been stuck on this for a while and would love to get the issue fixed.

+ 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. [SOLVED] Having trouble with VBA code trying to save copy of workbook PDF
    By sspreyer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2013, 09:59 AM
  2. Replies: 3
    Last Post: 08-07-2013, 10:16 AM
  3. trouble with copy rows code and loop
    By amartino44 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-15-2013, 04:34 AM
  4. [SOLVED] Need micro compare code in sheet2 and sheet3 with code in sheet1
    By jamalmail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2013, 05:15 PM
  5. Trouble writing a formula to compare/collect data from other cells/worksheets
    By tpavlak in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-07-2012, 04:13 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