+ Reply to Thread
Results 1 to 16 of 16

Combine multiple csv files and output one single csv file sorted by date

  1. #1
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Combine multiple csv files and output one single csv file sorted by date

    Hi! My code is given below. I am able to combine multiple csv files but struggling for two things:
    1. Sorting the data before the final output.
    2. Deleting unwanted data of specific dates before the final output. (T

    I am also attaching 3 sample csv files for your reference.

    The first column DisplayDate is in 'dd-mm-yyyy hh:mm:ss' format
    example : 20-03-2023 09:15:00 (20th March 2023)

    Desired result
    * The final csv file has dates sorted from newest to oldest. I want it the other way round - oldest to newest
    * Identify max date and delete all records which are not equal to max date. Out of the multiple csv files, there will be very few files which will have more than one date. If that is the case, keep data of max (newest) date and remove records of old dates.

    This code may not be efficient and perfect but it works. Please help me modify this code as per my desired result and if possible increase its efficiency and speed. Thank you

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by sabha; 07-02-2023 at 09:27 AM. Reason: solved

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

    Re: Combine multiple csv files and output one single csv file sorted by date

    try
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    Thank you for the quick reply.
    I tried your solution on the sample csv files that i attached but I am getting only 1 record and the same is repeated again.

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

    Re: Combine multiple csv files and output one single csv file sorted by date

    you said
    Please Login or Register  to view this content.
    What do you mean by this?

  5. #5
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    oh.. it was a communication error
    if you see ABC csv file, it has two dates 20-03-2023 10:00 and 19-03-2023 10:00. In this case, I want all records of 20-03 because that is the max date and delete the other record 19-03.

    For example, if it had records, the output should have first 3 records because that is the max date
    20-03-2023 10:00
    20-03-2023 09:50
    20-03-2023 09:40
    19-03-2023 10:00
    19-03-2023 09:50
    18-03-2023 10:00

    hope i have conveyed it properly

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Combine multiple csv files and output one single csv file sorted by date


    Hi,

    still unclear and as you forgot to attach the exact final expected csv text file !
    Do you want to keep only the max date whatever the source file or the max date for each file ?

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

    Re: Combine multiple csv files and output one single csv file sorted by date

    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    @Marc L sorry for not attaching the final expected CSV. I am attaching in this post
    @jindon I tried your last change and it worked as per my expectation but i lost 1 feature
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    If you see these lines in the initial code I posted, I use to get the stock name from the file name in the last column. This is missing. I am trying to integrate this in your code but since your approach is different, I am facing difficulty.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Combine multiple csv files and output one single csv file sorted by date


    Can you try to answer to post #6 question ?

  10. #10
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    Quote Originally Posted by Marc L View Post

    Hi,

    still unclear and as you forgot to attach the exact final expected csv text file !
    Do you want to keep only the max date whatever the source file or the max date for each file ?
    max date for each file. Sorry to not have answered your query.

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

    Re: Combine multiple csv files and output one single csv file sorted by date

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    Cool.... this is exactly what I wanted. Thank you so much

  13. #13
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    I marked this post as solved but there is one problem that needs attention.
    The code works just fine with the sample csv files but when I placed my actual csv files, I got error the below error
    Please Login or Register  to view this content.
    I understood the reason, it was happening because of the dot symbol (2000.00) in the file name. I removed the dot and it worked but in the last column, I got big name "ABC 27 JUL 2023 2000.00 PE" that is because of the underscore split whereas I just wanted "ABC"
    so I played with the following line without success.
    Please Login or Register  to view this content.
    I dont want to remove the dot in the filename. Without changing the filename, how can I avoid the error?

    Also let me know what changes I would need if I want to put the result in active worksheet instead of dumping it to a new csv file.

    Thank you

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Combine multiple csv files and output one single csv file sorted by date


    It's what happens when the attachment does very not well reflect the real files, even if it's only the filename …
    So you need to add a Split for the space.

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

    Re: Combine multiple csv files and output one single csv file sorted by date

    Please Login or Register  to view this content.
    Last edited by jindon; 07-02-2023 at 12:28 PM.

  16. #16
    Forum Contributor
    Join Date
    11-29-2014
    Location
    India
    MS-Off Ver
    MS Excel 2007
    Posts
    240

    Re: Combine multiple csv files and output one single csv file sorted by date

    Quote Originally Posted by Marc L View Post

    It's what happens when the attachment does very not well reflect the real files, even if it's only the filename …
    So you need to add a Split for the space.
    Sorry about that. It wasn't done intentionally. I thought sharing small sample files would be easy to share and understand. But now I think, it is important to share the actual data.

    @jindon Thanks once again. God bless !

+ 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] Combine multiple .csv, .ods, .xlsx files in a single workbook
    By PC41 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-21-2021, 12:05 PM
  2. How to Combine Multiples Files in a Folder into One Single File
    By KAlex13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-04-2019, 01:38 AM
  3. Replies: 0
    Last Post: 10-12-2015, 10:02 AM
  4. VBA code to insert data from multiple .txt files into one output file template file
    By psmith33 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-04-2015, 02:49 PM
  5. [Solved] Output multiple rows to a single row CSV file
    By stvgarner in forum Excel General
    Replies: 5
    Last Post: 02-06-2015, 02:06 PM
  6. Replies: 3
    Last Post: 09-03-2013, 11:53 AM
  7. [SOLVED] Extract a few values from several .csv-files and bundle them in a single output-file
    By Jeroen606 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 01:05 AM

Tags for this Thread

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