+ Reply to Thread
Results 1 to 16 of 16

Changing the structure of a csv file

  1. #1
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Smile Changing the structure of a csv file

    Dear members,

    I need your help to change the structure of csv file, A, to B in a new csv file, please.

    I tried to record macro but it was not useful. There are some missing values in dataset (blank cells), but I couldn't fill them in format A because of blank cells in Feb and other non-31-day months.

    I know how to run the code for the files in a folder, but for this part I need your assistance.
    Reagrds,
    Mori

    [File A]
    A1 B1 C1
    A2 B2 C2
    A3 B3 C3
    A4
    A5
    >> change structure to >>
    [File B]
    A1
    A2
    A3
    A4
    A5
    B1
    B2
    B3
    C1
    C2
    C3

    changing structure.png
    Attached Files Attached Files
    Last edited by Moriexcel; 11-23-2015 at 02:05 PM. Reason: Adding sample files & explanation

  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: Changing the structure of a csv file

    Please post at least one copy of each CSV, BEFORE.csv and AFTER.csv

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

    View Pic
    _________________
    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
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Dear JBeaucaire
    Thank you. I've added sample files and a few explanation.

  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: Changing the structure of a csv file

    This does all the formatting in memory so it should be pretty fast.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by JBeaucaire; 11-23-2015 at 01:57 PM.

  5. #5
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Wow, Thank you for the time. The program is so fast. But there are only two small issues, if you consider it please.

    1. The code crawls data by row rather than column. I.e. it reads Jan 1,1950 then Feb 1,1950 instead of Jan 1, 1950 then Jan 2, 1950 ...

    2. There are some blank cells, as missing value, other than months with less than 31-day. The code is ignoring the whole months with missing values. (e.g., Jun 1951)
    11-23-2015 2-15-30 PM.png
    I've used this code after running your code and it has inserted the missing dates. Since it is for 3 columns dataset, so I just inserted a blank column between columns date and value.
    Please Login or Register  to view this content.
    Last edited by Moriexcel; 11-23-2015 at 04:59 AM. Reason: tried to debug the code and report the problem

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

    Re: Changing the structure of a csv file

    1) I don't see a problem with #1. It is running row by row so the initial array created is in the monthly format, day by day, but at the end of the macro the data is sorted by date ascending putting the output back in calendar date ascending.

    2) The macro skips any cell with NO VALUE. It does not skip 0 value cells. What is the need in that regard?

  7. #7
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Yes. You' re right. There is no problem with #1. Sorry. It happens because I have used that code to insert missing dates.
    The problem with # two is, there are two types of blank cells in my dataset. 1st the cells for non-31-day months (e.g.,29-30 and 31 Feb) which program is perfect on this issue and 2nd, Real missing values (e.g., 1-30 Jun 1951). And the problem is, the code is ignoring those missing values, while I need those missing values in the NEW file.

    I tried to solve it myself to do not bother you more, but seems I failed to do so. Could you please help me on this as well. Thanks.

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

    Re: Changing the structure of a csv file

    I have edited one line of code above in red. That seems to fix #2.
    Attachment updated as well.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.
    Last edited by JBeaucaire; 11-23-2015 at 01:58 PM.

  9. #9
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Thanks a million.

  10. #10
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Hello again,
    I have a question, if you don't mind.

    How to do it for multiple files in a folder?
    Thanks.
    Last edited by Moriexcel; 12-13-2015 at 07:29 AM.

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

    Re: Changing the structure of a csv file

    So instead of picking a file at the beginning, you want to select a folder and have it process every Excel file in that folder?

  12. #12
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Yes please.
    I have actually recorded a macro, and I am trying to add your code to the end of that. Then I am going to add another macro to merge all created files by your code into one workbook.
    Last edited by Moriexcel; 12-14-2015 at 02:25 AM.

  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: Changing the structure of a csv file

    This version will make the original macro I wrote loop through all CSV files found in a chosen folder:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 12-13-2015 at 10:57 PM. Reason: Corrections

  14. #14
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Thank you. I think there is a problem in my side. The program cannot find csv files in the folder.
    Last edited by Moriexcel; 12-13-2015 at 03:38 PM.

  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: Changing the structure of a csv file

    My bad, a few typos, I've corrected them in the post above and tested, it is finding the CSV files now. Apologies.

  16. #16
    Registered User
    Join Date
    11-08-2014
    Location
    Malaysia
    MS-Off Ver
    2010
    Posts
    84

    Re: Changing the structure of a csv file

    Thank you very much. You are most helpful.

+ 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] Changing the structure of the files
    By Moriexcel in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-16-2015, 01:11 AM
  2. [SOLVED] Changing structure of data
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-02-2014, 08:59 AM
  3. Creating a Pyramid Hierarchy structure from a flat structure
    By thegamerulez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-16-2014, 01:28 AM
  4. Replies: 4
    Last Post: 12-12-2013, 09:49 AM
  5. CHeck boxes structure is changing when grouped into rows
    By kammariarun in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2013, 07:56 PM
  6. Cell style - changing ribbon structure
    By zlodiej in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2013, 02:17 PM
  7. VBA to change structure of excel file
    By Jedgroev in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-21-2012, 09:13 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