+ Reply to Thread
Results 1 to 18 of 18

Find cells, copy range with criteria and paste with offset

  1. #1
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Find cells, copy range with criteria and paste with offset

    Once again,

    too hard for me but I tried a lot


    My issue :

    I have a commun ID column (i.e column D) on both sheets (i.e sheet1, sheet2) with different data on each rows

    for each row x on sheet 2 I'd like to test if : it finds the common ID on sheet 2 + sheet1.range(E:X) is empty

    then select range(E:X) from sheet 2

    paste it in sheet1.Cells(D).offset or Sheet1.range(E:X)


    Important note : I have several ID with different data per sheets and I need to copy each ID from sheet2 to each ID in sheet1. it means when vba is searching for an ID in sheet 2, it shouldn't stop to the first value and to pick up the one without data in the E:X range as it where they are paste.

    Do you see what I mean ?

    On difficulty I encountered is to "call" the row where vba finds the ID in sheet1


    written lke this it doesn't sound really difficult to do but I got confused.

    This is one of the code I tried, but either it fails or it gives an error. Sorry I'm learning


    thank you

  2. #2
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Please Login or Register  to view this content.

    I have an issue with the range of cond1 and I'll probably have an other one for the .copy .paste code

    I keep going on it.

    thank


    ps : I adapted the code to my excel file format (i.e nbr of column to offset, etc.)


    I edited it, but I still have an issue with the range
    Last edited by benfontein; 02-09-2017 at 06:43 AM.

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    please attach your workbook
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  4. #4
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    thanks

    this is an extract of the "raw file"
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    You want to
    - copy columns E:X from sheet "chicks" to sheet "eggs"
    When
    - all cells in columns E:X in sheet "eggs" are empty AND ID in sheet "chicks" = ID in sheet "eggs"

    1) Is the above correct?

    2) There are different numbers of rows in the 2 sheets for each col/nest combination. WHY different no of rows?
    eg col8/nest16 has 2 rows in "chicks" and 3 rows in "eggs"

    3) There are multiple rows with the same ID which contain different values in some cells (applies to both sheets) - so how do we tell VBA what to copy and where to paste? What is the rule? (in words)

    Perhaps you could attach workbook with 3 sheets (create the 3rd sheet manually to show what you want to see)
    - sheet "chicks"
    - sheet "eggs" with range of empty cells in columns E:X
    - sheet "eggs AFTER paste"

  6. #6
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Quote Originally Posted by kev_ View Post
    You want to
    - copy columns E:X from sheet "chicks" to sheet "eggs"
    When
    - all cells in columns E:X in sheet "eggs" are empty AND ID in sheet "chicks" = ID in sheet "eggs"

    1) Is the above correct?

    Almost,

    1/ in fact exactly, it would be from E:BE from "chicks" to "eggs" (it was to make it easier with E:X for the example and I feel confident enough to edit your code )


    2/ In "eggs", te "paste range" is in Z :BZ (and not E:X) where all cells are empty and and the top row is in yellow.

    3/ ID "eggs" = Id "chicks" = column D



    2) There are different numbers of rows in the 2 sheets for each col/nest combination. WHY different no of rows?
    eg col8/nest16 has 2 rows in "chicks" and 3 rows in "eggs"
    They are different because not all eggs hatched. If an egg hatches it will have a row in "chicks"


    3) There are multiple rows with the same ID which contain different values in some cells (applies to both sheets) - so how do we tell VBA what to copy and where to paste? What is the rule? (in words)
    there is no proper rule. Which means, a chick doesn't have to be link with a precise egg !

    A chick could be pasted to any egg of the same ID... I will adapt every thing manually afterwards.


    Perhaps you could attach workbook with 3 sheets (create the 3rd sheet manually to show what you want to see)
    - sheet "chicks"
    - sheet "eggs" with range of empty cells in columns E:X
    - sheet "eggs AFTER paste"

    please consider the following workbook.
    I did it until col 8 nest 13_2

    thank you
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    I am having terrible problems accessing the forum and posting today. So this is my last post for a few hours...

    Which means, a chick doesn't have to be link with a precise egg !

    A chick could be pasted to any egg of the same ID... I will adapt every thing manually afterwards.
    BUT
    Column AE in sheet "eggs" is the Hatching Date - so how about we apply the rule like this:

    FOR each ID in sheet "eggs"
    IF all cells in columns Z:BZ are empty (all rows for that ID)
    AND
    column AE is not empty

    THEN for each ID in sheet "chicks"
    - find next matching ID
    - copy that row for the ID (columns E:BE only)
    - paste to sheet "eggs" (columns Z:BZ)

    EDIT
    How can Z:BZ be empty ? AE should have values for any eggs that hatch
    So which columns in sheet "eggs" are we checking are empty?
    Last edited by kev_; 02-09-2017 at 01:09 PM.

  8. #8
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    thanks for your help

    Column AE in sheet "eggs" is the Hatching Date
    in this project we consider only one hatching date per nest, so don't trouble yourself too much with it


    FOR each ID in sheet "eggs"
    IF all cells in columns Z:BZ are empty (all rows for that ID)


    THEN for each ID in sheet "chicks"
    - find next matching ID
    - copy that row for the ID (columns E:BE only)
    - paste to sheet "eggs" (columns Z:BZ)
    like this it sounds perfect I guess



    EDIT
    How can Z:BZ be empty ? AE should have values for any eggs that hatch
    So which columns in sheet "eggs" are we checking are empty?
    As I said, only one value per nest for the hatching. only the first hatched egg matters.


    does that help ?



    thank you very much once again

    i've been busy this evening sorry
    I took a leave for 2 weeks and I won"t be able to check my emails on regularly basis but tomorrow I'll work on it, I want it to be done.

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    Try the macro in the attached workbook

    I had to insert column D in sheet "Chicks" with same formula as column D in sheet "Eggs" to get a matching ID value.
    This means that we now copy from F:BF
    The line below is not necessary - but it marks what has been amended - delete after testing
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kev_; 02-10-2017 at 08:09 AM.

  10. #10
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Hi kev_


    I apologise to come back on this thread 2 weeks after, I would have prefer it to be done before I left ..


    We might had a misunderstanding because what you've done differs slightly from what I expected. I must have been confused at some point, hence let me explain it again :


    each row in chicks should find a match in egg's sheet but as there is more eggs than chicks, some rows in eggs have to remain empty.
    In the previous code you did, it is always the first chick's ID which is pasted as many times as there is rows in eggs and as long as it matches, which is not what I expected.


    My idea was :

    for each rows in chicks
    find a match where e.range(Z:BZ) is empty and paste c.range(F:BF) in e.Z

    My idea with the empty cells was to avoid overwriting chick's data on a egg's range which already found a match (but maybe it is useless ?)


    By this way, I thought every chicks will found a match and some rows in Egg will remains empty as there is less chick's rows than egg's.


    I'll try to edit you code while you could find a solution


    many thanks again for your help.

    I hope I make it clear.

  11. #11
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    I edited your code as following

    I think it could work but I might have an issue with the "on error resume next" because nothing is happening ..


    Please Login or Register  to view this content.

    what do you think ?

  12. #12
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    No problem - I can look at this for you tomorrow.

    I cannot remember all the details now...

    Let's take an example with 7 eggs and 4 chicks and one row in "chicks" with a match
    - in "eggs" I think you want 4 rows with data and 3 blanks is this correct?
    - 3 more rows need to be updated - is this correct?
    If so, VBA needs to count how many rows in "eggs" (for that ID) have blanks to know how many rows to update
    In the example
    - first row of ID - check count of blanks in "chicks" = 6
    - is this > 3 ? YES - update row
    - next row - check count of blanks in "chicks" = 5
    - is this > 3 ? YES - update row
    - check count of blanks in "chicks" = 4
    - is this > 3 ? YES - update row
    check count of blanks in "chicks" = 3
    - is this > 3 ? NO - GOTO next ID

    Will that work?

  13. #13
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Let's take an example with 7 eggs and 4 chicks and one row in "chicks" with a match
    first of all, all rows in chicks are matching with a row in Eggs


    - in "eggs" I think you want 4 rows with data and 3 blanks is this correct?
    - 3 more rows need to be updated - is this correct?

    Depending from where these chicks are, I would like to have these 4 rows in eggs. For the unmatched cells, I'd like they remain empty.

    If so, VBA needs to count how many rows in "eggs" (for that ID) have blanks to know how many rows to update
    In the example
    - first row of ID - check count of blanks in "chicks" = 6
    - is this > 3 ? YES - update row
    - next row - check count of blanks in "chicks" = 5
    - is this > 3 ? YES - update row
    - check count of blanks in "chicks" = 4
    - is this > 3 ? YES - update row
    check count of blanks in "chicks" = 3
    - is this > 3 ? NO - GOTO next ID
    compare to my answers it seems to me easier than this, no ??

    I'll attach a table
    Attached Files Attached Files
    Last edited by benfontein; 03-01-2017 at 06:04 AM. Reason: attached a file

  14. #14
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Hi Kev_,

    sorry for asking but did you find something ? I'm struggling with this code

    Thank you

  15. #15
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    I was a bit confused by your answers in post#13

    Can you send a workbook with 3 sheets with several examples
    - chicks
    - eggs before VBA
    - eggs after VBA

    thanks

  16. #16
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Hi,

    I hope it will help


    thanks
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    10-11-2015
    Location
    france
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    83

    Re: Find cells, copy range with criteria and paste with offset

    Finally, I decided to start this process manually.

    No worries
    thank you for your help


    fk

  18. #18
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Find cells, copy range with criteria and paste with offset

    Have a look at the attached workbook
    Look in sheet "Eggs" columns CA to CE

    I have added formula in those columns to see if this will give VBA a method to do what you want
    (we will not need the formula in the cells - this is just to make sure we get it correct later)

    VBA will need to do something like

    On each row check if the cumulative "count" of columnZ is less than cumulative "count" of chicks then update this row
    If there are more eggs than chicks then do not update row when cumulative "count" of eggs > cumulative "count" of chicks)

    Have a look. And let me know.

    I am away from my PC for a few days
    Attached Files Attached Files

+ 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] Help! - Copy Range (Sheet1,A10:F50), Paste Range (Sheet 2,A1:F41), Offset Range & Repeat
    By cjtimmer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2015, 06:27 PM
  2. [SOLVED] Vba : Find select offset copy and paste
    By Atik in forum Excel General
    Replies: 6
    Last Post: 12-21-2014, 11:38 AM
  3. how to copy a range of cells, paste them and then find next empty cell
    By comp23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2013, 03:34 PM
  4. Copy/Paste range of cells to another worksheet based on matching criteria
    By maa50904 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2013, 12:07 AM
  5. VBA macro to Find a dynamic range of cells, copy then paste to another sheet
    By Bmxerdude2087 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2012, 02:36 PM
  6. Help with find, copy, and paste code using offset
    By mundellj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2011, 08:16 PM
  7. [SOLVED] Loop Through A Range, Find Cells > 1, Copy Offset To Another Sheet
    By bugmenot in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-25-2008, 06:30 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