+ Reply to Thread
Results 1 to 24 of 24

Copy row of any cell with date in the past and paste to new sheet

  1. #1
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Copy row of any cell with date in the past and paste to new sheet

    I need some help with creating a VBA that will scan my entire sheet and any cell with a date before todays date, the entire row will be copied and pasted to another sheet. and it should search every cell in sheet 1 and paste all rows with dates in the past. if there is more then one cell in a row with the date in the past, that row will copied only once.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy row of any cell with date in the past and paste to new sheet

    Give this a try:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy row of any cell with date in the past and paste to new sheet

    John, notice your method does not preclude a single row being copied multiple times if there are several old dates in that one row?

    Look at the method I've presented, we create a "range" as we find cells instead of copying, when we're done checking cells, all the rows in the created range are copied all at once, one time, thus each row will only get copied once in the case of multiple old dates.

    Performance-wise, it should be much faster overall to copy once than to copy rows individually.

  5. #5
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    Hey JOHN,
    I tried that code and for some reason it goes from the bottom of the sheet to the top. i would want something that goes top down. and for some reason it is moving almost all rows

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Hi Jerry: Performance-wise you have a much better solution. My method loops each row and then for each cell in that row "1 to columns.count", if it finds a cell < date it copies that cells row and goes too the next row. Multiple cells in the same row would be avoided with the Goto command, wouldn't it?

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Quote Originally Posted by jeetamin View Post
    Hey JOHN,
    I tried that code and for some reason it goes from the bottom of the sheet to the top. i would want something that goes top down. and for some reason it is moving almost all rows
    Did you try Jerry's code? Maybe it will work for you. Otherwise, please attach a sample file with some data to test on.

  8. #8
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    JBeaucaire,
    Your code gives me an error saying that there is a type mismatch..

  9. #9
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    here is a sample file...i need the rows with dates in the past to be pasted to new sheet...
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    This copies to Sheet2.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    the problem that is arising is that in the master file there are cells with other values in it..such as prices which is messing up the code. is there a way to only do the scan on certain selected files within the sheet?

  12. #12
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    You can incorporate IsDate as Jerry did in his solution.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy row of any cell with date in the past and paste to new sheet

    Here's the correction:
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    Thanks guys! that seems to work. Now if I wanted to add a condition such as if there was a cell that said "Q M" then that row should also be pasted to new sheet how would i go about that? again i dont want a row to be pasted twice in sheet 2.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy row of any cell with date in the past and paste to new sheet

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    this doesn't seem to be working..is there any way to add it to JOHN H. DAVIS's code?

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy row of any cell with date in the past and paste to new sheet

    I'll let John adapt his code. Mine looks like it should work. If you care to post a sample workbook with my code in it and show me what it's doing wrong for you....

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  18. #18
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Maybe:

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    when I try that JOHN I get error message "Type Mismatch"

  20. #20
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Quote Originally Posted by jeetamin View Post
    when I try that JOHN I get error message "Type Mismatch"
    I can't duplicate the error with your previous attachment. But try:

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    That works but if there is a row with a Q M and a late date it will pull the row twice. is there a way so that the rows are not duplicated?

  22. #22
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    Try:

    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    01-15-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Copy row of any cell with date in the past and paste to new sheet

    Works like a charm! Thank you for all your help!!

  24. #24
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy row of any cell with date in the past and paste to new sheet

    You're welcome. Glad to help out and thanks for the feedback. Please comply with Forum Rule No. 9.

    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

+ 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. I created a cut & past VBA and trying to modify it for copy and paste
    By vademo0o in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2013, 10:14 AM
  2. Copy/paste row with todays date in Sheet2 to sheet 3....but from Sheet 1
    By cammyjane10 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-25-2013, 01:07 PM
  3. Replies: 4
    Last Post: 11-20-2012, 05:25 PM
  4. copy template,past new sheet and rename to given date for 30 days
    By fasalazar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2012, 05:19 PM
  5. Replies: 8
    Last Post: 03-01-2012, 02:44 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