+ Reply to Thread
Results 1 to 13 of 13

Copy rows that meet weekday criteria to different worksheets

  1. #1
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Copy rows that meet weekday criteria to different worksheets

    Hi all

    I am looking for the quickest method to copy rows of data that have a date in the first column.

    The idea is to copy (in date order) any weekday that is a : -

    Monday into the Monday worksheet
    Tuesday, Wednesday and thursday into the Midweek worksheet.
    Friday into the Friday worksheet.

    I have potentially 50 to 100 thousand rows with data as below.

    20/04/2020 06:35 0.63429 0.63429 0.6339 0.63399 40
    20/04/2020 06:40 0.63402 0.63413 0.63386 0.63399 40
    20/04/2020 06:45 0.63398 0.63409 0.63385 0.63385 13
    20/04/2020 06:50 0.63379 0.63396 0.63379 0.63387 28
    20/04/2020 06:55 0.63392 0.63424 0.6339 0.63421 25

    Is it best to add a row with an =Weekday(a1) into a new column and then loop through the rows to copy each to the respective sheet.

    Or to do a row by row check on the date and copy during the loop?

    Or is there a quicker way? Perhaps using filter?

    Thanks

    Neil

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    I recommend adding a weekday column and then using autofilter to select all the common days.

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    Here is some code for your situation.

    Please Login or Register  to view this content.
    Last edited by maniacb; 10-15-2020 at 09:40 AM. Reason: Added cleanup lines to code

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    This iteration address the "midweek" requirement

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Copy rows that meet weekday criteria to different worksheets

    Hi Maniacb.

    This works wonderfully thank you so much.

    It has left me with a slight problem in that i had forgotten that i had formatted the A1 column to the date format dd/mm/yyyy from a text date i.e. 2020.04.23 format.

    Your code then uses my formatted date to do the work.

    To do this i used

    Please Login or Register  to view this content.
    Is there a way to use the text date "as is" in your code.

    I have tried incorporatiing it but date formatting in excel is a bit of a minefield to me.

    Ultimately i will need to leave the A column as is and delete the added columns. I have coded the column deletes in your "with - end with" loop (Columns("H:J").EntireColumn.Delete)

    The program the csv file is used in can only have dates of type yyyy.mm.dd as shown.

    BTW on checking the format of the column a as received it comes up as General if that helps.

    Thanks for your help.

    Regards

    Neil
    Last edited by webwyzard; 10-15-2020 at 12:32 PM.

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    Can you post a snippet of your original file so we can get the column designations correct?

  7. #7
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Copy rows that meet weekday criteria to different worksheets

    The original is a csv, sample attached.

    I also added,

    Dim sht As String
    sht = ActiveWorkbook.Worksheets(1).Name

    To use the current workbook / first worksheet name in your code.

    I am curently writing code to save each worksheet as the original filename and a suffix dependent on each of the worksheets created.

    Thanks

    Neil
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    Alrighty, this code worked for me, But the file needs a title line I typed in manually

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Copy rows that meet weekday criteria to different worksheets

    Hi maniacb this worked brilliantly but the date column "a" changed it's format to dd/mm/yyyy.

    It may be the way I explained it, but i need it to remain (or change back to) yyyy.mm.dd so it can be used in the program it came from.

    Could it be a UK default date format problem?

    Thanks

    Neil
    Last edited by webwyzard; 10-15-2020 at 06:37 PM.

  10. #10
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    Oops, I changed it. We can change it back. I’ll update the code

  11. #11
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Copy rows that meet weekday criteria to different worksheets

    After studying the code more I managed to get it to work by changing both the formatting parts e.g. .NumberFormat = "mm/dd/yyyy" to .NumberFormat = "yyyy.mm.dd" and the same for the Format(Range("A" & i).Value, "mm-dd-yyyy")

    Thanks
    Neil

  12. #12
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Copy rows that meet weekday criteria to different worksheets

    So You all good?

  13. #13
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: Copy rows that meet weekday criteria to different worksheets

    Yes i am all good. Though my other half might disagree

    thank you again.

    regards

    Neil

+ 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] Copy and paste between two workbooks where rows meet criteria
    By dubcap01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2015, 07:38 AM
  2. [SOLVED] Copy & Reorder Columns of Rows That Meet Criteria
    By stusic in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2015, 11:19 AM
  3. VBA Macro to copy rows that meet certain criteria
    By todd.rossi82 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-29-2014, 12:38 PM
  4. Copy rows from all worksheets in workbook that meet 2 criteria
    By Hugh Peterson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2014, 01:35 PM
  5. Copy Rows that meet criteria to another workbook that contains all of them
    By TubbzUK in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2013, 06:23 AM
  6. Copy Rows That Meet Given Criteria
    By BudParker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2008, 05:00 PM
  7. Copy rows that meet a certain criteria
    By James in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2006, 06:35 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