+ Reply to Thread
Results 1 to 18 of 18

Copy and Paste Cells If Match Found

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Copy and Paste Cells If Match Found

    Hi all,

    This one is beyond my level but I would very much like to build it into my spreadsheet, so I would appreciate your help.

    I have a spreadsheet (range A1:P5000). B2:B5000 would contain cheque numbers. Many of the cheque numbers would be repeated and would have common data in columns C, D, E, F, G, H, L and N.

    I am trying to get VB code to copy and paste the common data when a user enters a cheque number.

    For example:

    When a user enters a cheque number in B3, VB would check B2 for a match. If a match is found, then VB would copy C2, D2, E2, F2, G2, H2, L2 and N2 and paste them in C3, D3, E3, F3, G3, H3, L3 and N3. If no match is found, then the user would have to manually enter the data in C3, D3, E3, F3, G3, H3, L3 and N3.

    When a user enters a cheque number in B4, VB would check the B2:B3 for a match. If a match is found (in B2 – for example), then VB would copy C2, D2, E2, F2, G2, H2, L2 and N2 and paste them in C4, D4, E4, F4, G4, H4, L4 and N4. If no match is found, then the user would have to manually enter the data in C4, D4, E4, F4, G4, H4, L4 and N4.

    When a user enters a cheque number in B100, VB would check the B2:B99 for a match. If a match is found (in B90 – for example), then VB would copy C90, D90, E90, F90, G90, H90, L90 and N90 and paste them in C100, D100, E100, F100, G100, H100, L100 and N100. If no match is found, then the user would have to manually enter the data in C100, D100, E100, F100, G100, H100, L100 and N100.

    Thank you very much,
    Gos-C
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Paste the following macro into the tab corresponding to the data sheet in the VBA editor (shift F11)

    Please Login or Register  to view this content.
    Martin

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Gos-c,

    Just to clarify, in your post you said...
    I have a spreadsheet (range A1:P5000). B2:B5000 would contain cheque numbers. Many of the cheque numbers would be repeated and would have common data in columns C, D, E, F, G, H, L and N.
    If a cheque number appears more than once in the list, how do you determine which one to use?

    Sincerely,
    Leith Ross

  4. #4
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    mrice, I am getting "Object variable or With block variable not set." Also, can you please code it to use Option Explicit.

    Leith, it can be the first match - the data to be copied will be the same for all the matches.

    Thank you,
    Gos-C

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Gos-c,

    Copy this macro into a Standard VBA module. You can then add a call to it from any worksheet's Change event procedure.
    Please Login or Register  to view this content.
    Calling Example
    The worksheet will invoke the macro whenever the user types in a cell in column "B". The macro will search from the start row to the cell row above the active cell for a match. If the data match then the specific cells will be copied.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 09-06-2008 at 04:25 AM.

  6. #6
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Thumbs up

    Great! It works perfectly.

    Thank you very much, Leith - greatly appreciated.

    Gos-C

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Here's a non-looping version.
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Leith,

    After a few more testing, I found a problem.

    I entered 123456 as the first cheque number, and when I entered 123456 as the second cheque number the data was copied as expected.
    I entered 23456 as the next cheque number, and nothing is copied - as expected, but when I entered 23456 as the fourth cheque number, the data for cheque number 123456 (instead of the data for cheque number 23456) was copied.

    Similarly, when I entered 3456 as the next cheque number, nothing is copied - as expected, but when I entered 3456 as the sixth cheque number, the data for cheque number 123456 (instead of the data for cheque number 3456) was copied.

    Any fix?

    mikerickson, I haven't tried your submission as yet. I will do so immediately after posting this, and then I'll let you know how it worked.

    Thanks,
    Gos-C

  9. #9
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    mikerickson, I pasted the code in the sheet module but it's not working.

    Gos-C

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Is the B entry the only thing in that row? If not, it won't trigger.
    If you want this to happen if there is already a column A entry, edit this line:
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Thumbs up

    Right on!

    I have a formula, =IF(B5<>"",COUNTA($B$2:B5),""), in column A that inserts the row number.

    Thank you very much,
    Gos-C

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    If you want to be able to edit existing cheque numbers (column B) without triggering the substitution (and there are no other formulas,etc in the row), you could change that line to
    Please Login or Register  to view this content.
    As is, the routine is triggered whenever any cell in column B is changed, even when there is existing data in that row.

  13. #13
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Thank you, mikerickson. That's good to know.

    Gos-C

  14. #14
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi all,

    When I entered the first cheque number (or one that hasn't been previously entered), #N/A is being filled in C, D, E, F, G, H, L, N, and O. I can't figure out how to fix it. Can someone please help me.

    BTW, I modified the code as follows:

    Please Login or Register  to view this content.
    Thank you,
    Gos-C

  15. #15
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hello,

    I have attached a copy of my workbook. Can anyone tell me how to prevent #N/A from appearing in the cell? I want the cells to be blank, if the cheque number is not found.

    Thank you,
    Gos-C
    Attached Files Attached Files
    Last edited by Gos-C; 09-09-2008 at 09:20 AM.

  16. #16
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi everyone,

    Any help? I have attached the file.

    Thanks,
    Gos-C

  17. #17
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Gos-C,

    Cross posting without the link. Please add the link

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  18. #18
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi VBA Noob,

    Sorry about that. I entered the link to this posting on this forum on the other forum but forgot to do the reverse here.

    Here it is:

    http://www.mrexcel.com/forum/showthr...=1#post1681237

    Gos-C

+ 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. copy and paste without overwriting cells containing values
    By schueyisking in forum Excel General
    Replies: 2
    Last Post: 08-14-2008, 05:43 AM
  2. Copy & paste from cells below named ranges
    By Glio in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-19-2007, 08:15 AM
  3. Select a range of cells and then copy them, and paste into other cells
    By Dynelor in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-24-2007, 04:03 PM
  4. Copy A Range of Cells Or Sheet and Paste to A New Workbook
    By Nathan123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2007, 09:24 PM
  5. Protect Cells from Copy and Paste
    By kbraaten in forum Excel General
    Replies: 3
    Last Post: 11-29-2006, 04:43 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