+ Reply to Thread
Results 1 to 25 of 25

Copy rows from Workbook 1 to Workbook2 if condition is met

  1. #1
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Copy rows from Workbook 1 to Workbook2 if condition is met

    If there is a formula or function to this outside of VBA or macro, I would rather go that way but I don't think there is. Even the VBA seems to be a doozy. I found similar questions but I cannot adapt the codes to my workbook for them to work properly, or at all.

    In my Worksheet 1, currently named "Master" I have thousands of entries. When serial number in column A matches a serial number in another worksheet called "Reports", the date associated to the matched serial number in the "Report" worksheet is displayed in column P of the Master worksheet.

    I now need to have all the rows that does not contain a date in column P of the Master worksheet to be copied in a separate worksheet. There is a received date column in the Master worksheet and the condition to copy the rows without a date in P needs to also include the rows with a date in P that is less recent than the date in D.

    In other words, I need a worksheet to display all the rows from the Master worksheet except the rows with a date in P or a date in P that is later than the date in D.

    It is not a one time procedure so I need the new Worksheet to be updated as the Master receives new entries or dates in column P are being populated.

    Thanks again for the great help!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Here it is
    Attached Files Attached Files

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Try this:

    Please Login or Register  to view this content.
    * I changed Sheet1 to "Master"
    Last edited by xladept; 02-26-2018 at 05:18 PM.

  5. #5
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Thanks, this is on the right track and useable. Is there any way to have it keep the source formatting? I reformatted the destination worksheet but as soon as I run the module again for an update, it goes back to a set format for the destination. Also, it seems like I have to run the module to have the worksheet updated with new entries or modifications from Master. If it's not possible to have it updated dynamically or automatically, how can I attribute a shortcut key for the module to run, i.e. Ctrl+2?

    Thanks again so much!

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

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Try
    Change sheet names, if needed.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Thanks Jindon... xLAdept code was working a bit better. Yours does copy partial formatting for column A, but it only shows data up to row 61 in column A.

    I need exactly what Xladept had done, but I need a shortcut key if it can't update automatically and I would need the source formatting to be copied as well, or to preserve the formatting that I will put in place for the destination worksheet. Currently, if I set all the formatting manually after I ran the module, all the formatting is reset if I run the module again for update.

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

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    If you are talking about the data other than you uploaded, no idea.
    Attached Files Attached Files
    Last edited by jindon; 02-27-2018 at 10:05 AM.

  9. #9
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Unfortunately, my sample workbook cannot be anything like my actual workbook. I'm also trying to have the refreshed Result worksheet to appear as the first tab instead of the last.
    I suppose the following is the line to modify but I can't figure out how.
    Please Login or Register  to view this content.
    I also tried to play with the following code line, without success, in an attempt to keep the source formatting in regards to column width, however ideally I would like to keep all the formatting from the source, including cell size and fill colors, etc...

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Also, I managed to associate the module to a short cut key using this:
    Please Login or Register  to view this content.
    So this is not an issue anymore. Most important thing would be to be able to keep formatting and column size from the source workbook, then have the tab populated at the beginning instead of at the end.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Try this:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    FANTASTIC! Simply wonderful! I wish I could understand that coding stuff because I would adapt this for many other usage.

    I see where the conditions are coded and I'm trying to modify them to generate worksheet for other conditions. If I'd like to generate another worksheet, let's call it "Quick MEP" , that contains all rows showing "Quick Hits" in column I AS WELL AS "Yes | Oui" in column J, how would I code these conditions in replacement of:
    Please Login or Register  to view this content.
    After that I will try to figure this stuff out by myself. If I can understand how to input the conditions, I could use that coding to generate many other kinds of useful reports.

    Many thanks again for the great help!

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    You're welcome and thanks for the rep!

    Please Login or Register  to view this content.
    Last edited by xladept; 02-27-2018 at 02:10 PM.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    * change the Or to and And - already changed in last post

  15. #15
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Thanks again,

    I'm not able to add that code line to return the expected results. AS id it does not return anything expect the colors in the top rows. I changed the "Or" command by "And", and that gives me all the entries with Yes in column J regardless of what is in column I. It would be more important to retrieve the "Quick Hits" in column I so I tried with only the first part and it's not working. I also had to remove the End If because it was giving an error: End If without If block...

    Here's what I tried:
    Please Login or Register  to view this content.
    This gives me all rows with Yes in J, regardless of Quick hits in I. Weirdly, it removed text from some of my header cells.

    I also tried:
    Please Login or Register  to view this content.
    This gives me the exact same results as the previous code

    And I tried this, which I thought would simplify things but does not return any entries:
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Oh, and I also tried replacing the <> by = and it returned all the entries. Seems like I'm not getting that whole VBA thing... admittedly, the last programming I did was using Basic code on a commodore 64

  17. #17
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    I threw a byte switch in and it seems to be working:

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Thanks for trying, but it provides the same results. I will quit on this idea as I feel like I'm abusing of your good will. I'll have to take on learning VBA coding in the near future

    It boggles me how changing one argument in the code seems to have impact on everything else. i.e. in the Results code you gave me, all the formatting and the 2 top rows which serves as headers stays the same and is perfect. But in all the attempts for the MEP code, the text in the 2 top rows serving as headers is replaced by the actual data.

    It simply does not make sense to me how modifying the If line to change the argument has an impact on my headers... anyhow... maybe it will make sense to me one day!

    Thanks again for all the help!

  19. #19
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    But it didn't give the same results on my testing

  20. #20
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Wish I could help, but I'm more at a loss than you are The only thing I can say is that I have both versions in 2 separate modules and I get the same Worksheet with the same entries regardless of which one I run. It pulls out 195 entries, so I could compare. It should actually pull out 18 out of the 195 that has a Yes in J. I have the count for how many has Yes in J and Quick hits in I, but I'm stuck at not being able to pull them from the master onto a different worksheet.

  21. #21
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    If J has only a "Yes" then the code won't grab it, we're testing for "Yes | Oui" how about another sample workbook?

  22. #22
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Sorry about the confusion. J does have "Yes | Oui"... I was using Yes as short for "Yes | Oui"

  23. #23
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    So, I managed to set things up in a workable way. First, although it doesn't logically make sense, I got the results I initially wanted (Yes in J and Quick hits in I) by using OR as the operator instead of AND. I know, does not make sense but it works.

    I fixed half of the formatting issue by changing the first digit in the last line : ws.Cells(1, 1).Resize(s - 1, c) = Q to make it ws.Cells(3, 1).Resize(s - 1, c) = Q.
    This made the entries to be copied starting at row 3 leaving the first 2 rows of headers empty in txt but colored and sized as the source worksheet. I'm loosing the text for the headers but I can live with that.

    So I ended up making 4 worksheet generator codes in modules.

    1- INVENTORY, To pull all rows with no dates in P or a date in P that is earlier than a date in P.
    This works 100%, I get the headers' text copied as well, probably because the test picks it up in the lines to be copied. Here's the code:
    Please Login or Register  to view this content.
    2- MEP, Pulling the rows with "Yes | Oui" in column J. Works good, the only flaw is the headers' text does not get copied.
    Here's the code:
    Please Login or Register  to view this content.
    3- QUICKHITS, Pulling all the rows with "Quick hits" in column I. Again, works good, the only flaw is the headers' text does not get copied.
    Here's the code:
    Please Login or Register  to view this content.
    4- QUICKMEP, pulling rows with "Yes | Oui" in column JAND "Quick hits" in column I.... but using OR as a logical operator?!?!?!?
    Here's the code:
    Please Login or Register  to view this content.

  24. #24
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Here, on my system, the inclusion of the byte switch fixed the logical discrepancy - but I see that you're not using it.

    Losing Headers? - maybe:

    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    01-31-2018
    Location
    Bathurst, NB
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy rows from Workbook 1 to Workbook2 if condition is met

    Well thank ya! that was all I needed!

+ 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 rows from one workbook1 to a closed workbook2 if a condition is met
    By Vicky2413 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-18-2017, 03:45 AM
  2. [SOLVED] Copy all rows from one Workbook1.Sheet2 to Workbook2.Sheet3 and append them
    By dubcap01 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2015, 06:18 AM
  3. How to bring data from workbook1 to workbook2 by comparing values of workbook2
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2013, 09:38 AM
  4. To copy specific rows after passing a condition into another workbook.
    By aysrun in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-10-2013, 03:15 AM
  5. macro to copy rows to another sheet based on condition mentioned in another workbook
    By sona_tejas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-07-2013, 06:23 AM
  6. copy rows to specific worksheets in another workbook based on a condition
    By AngelaM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-22-2012, 12:36 PM
  7. Data from workbook1 to workbook2 every week but in different place in workbook2
    By cheelie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2007, 06:26 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