+ Reply to Thread
Results 1 to 16 of 16

Changing the structure of a csv file

Hybrid View

Moriexcel Changing the structure of a... 11-22-2015, 04:33 AM
JBeaucaire Re: Changing the structure of... 11-22-2015, 04:43 AM
Moriexcel Re: Changing the structure of... 11-22-2015, 01:27 PM
JBeaucaire Re: Changing the structure of... 11-22-2015, 04:19 PM
Moriexcel Re: Changing the structure of... 11-23-2015, 02:20 AM
JBeaucaire Re: Changing the structure of... 11-23-2015, 01:16 PM
Moriexcel Re: Changing the structure of... 11-23-2015, 01:46 PM
JBeaucaire Re: Changing the structure of... 11-23-2015, 01:55 PM
Moriexcel Re: Changing the structure of... 11-23-2015, 02:05 PM
Moriexcel Re: Changing the structure of... 12-13-2015, 06:09 AM
JBeaucaire Re: Changing the structure of... 12-13-2015, 01:24 PM
Moriexcel Re: Changing the structure of... 12-13-2015, 02:06 PM
JBeaucaire Re: Changing the structure of... 12-13-2015, 02:35 PM
Moriexcel Re: Changing the structure of... 12-13-2015, 02:55 PM
JBeaucaire Re: Changing the structure of... 12-13-2015, 10:59 PM
Moriexcel Re: Changing the structure of... 12-14-2015, 02:24 AM
  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.
    Option Explicit
    
    Sub CSVconverter()
    Dim wbCSV As Workbook, MyArr As Variant, NewArr As Variant
    Dim LR As Long, NR As Long, i As Long, j As Long
    
        With Application.FileDialog(msoFileDialogFilePicker)
            .Filters.Clear
            .AllowMultiSelect = False
            .Filters.Add "CSV Files", "*.csv", 1  'default
            .ButtonName = "SELECT"
            .Title = "Select CSV file"
            .Show
            If .SelectedItems.Count > 0 Then
                Set wbCSV = Workbooks.Open(.SelectedItems(1))
            Else
                Exit Sub
            End If
        End With
    
        LR = Range("A" & Rows.Count).End(xlUp).Row
        MyArr = Range("A1:N" & LR).Value
        ReDim NewArr((WorksheetFunction.Count(Range("A1:N" & LR)) + 1000), 2)
        NewArr(0, 1) = "Date": NewArr(0, 2) = "Value"
        NR = 1
    
        For i = 2 To UBound(MyArr, 1)
            For j = 3 To 14
                If IsDate(MyArr(1, j) & " " & MyArr(i, 2) & ", " & MyArr(i, 1)) Then
                    NewArr(NR, 1) = Format(DateValue(MyArr(1, j) & " " & MyArr(i, 2) & ", " & MyArr(i, 1)), "MM/DD/YYYY")
                    NewArr(NR, 2) = MyArr(i, j)
                    NR = NR + 1
                End If
            Next j
        Next i
    
        Sheets.Add
        Range("A1:C" & UBound(NewArr)).Value = NewArr
        Range("A:A").Delete xlShiftToLeft
        Range("A:B").Sort Range("A2"), xlAscending, Header:=xlYes
        ActiveSheet.Move
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:=Replace(wbCSV.FullName, ".csv", "-NEW.csv"), FileFormat:=xlCSV, CreateBackup:=False
        ActiveWorkbook.Close False
        wbCSV.Close False
        
    End Sub
    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.
    Sub insert_missing_dates_modified()
    'TXT2colm recorded macro to adjust date col
    'MDY to DMY to prevent mismatching error
    Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            FieldInfo _
            :=Array(1, 4), TrailingMinusNumbers:=True
    '
    'Original Code for inserting missing dates
    Dim b As Object, c, q As Long, r As Long
    Dim u(), x, y
    r = Cells(Rows.Count, 1).End(xlUp).Row
    x = Cells(3, 1): y = Cells(r, 1)
    Set b = CreateObject("scripting.dictionary")
    ReDim u(1 To y - x + 1, 1 To 3)
    For Each c In Range("A3:A" & r).Value
        b(c) = 1
    Next c
    For c = x To y
        If Not b(c) = 1 Then
            q = q + 1
            u(q, 1) = c
            u(q, 3) = "NA"
        End If
    Next c
    If q > 0 Then
        Cells(r + 1, 1).Resize(q, 3) = u
        Cells(3, 1).Resize(r - 2 + q, 3).Sort Cells(3, 1), Header:=xlNo
    End If
    MsgBox q & " rows inserted"
    End Sub
    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:

    Option Explicit
    
    Sub CSVconverter()
    Dim csvPATH As String, csvNAME As String, wbCSV As Workbook, MyArr As Variant, NewArr As Variant
    Dim LR As Long, NR As Long, i As Long, j As Long
    
        With Application.FileDialog(msoFileDialogFolderPicker)      'Folder selection
            .AllowMultiSelect = False
            .InitialFileName = ThisWorkbook.Path
            .Title = "Select a folder with CSVs to convert"
            .ButtonName = "Choose this CSV folder"
            .Show
            If .SelectedItems.Count > 0 Then csvPATH = .SelectedItems(1) & "\" Else Exit Sub
        End With
    
        csvNAME = Dir(csvPATH & "*.csv")                            'get first filename
        
        If Len(csvNAME) = 0 Then                                    'make sure at least 1 was found
            MsgBox "No CSV files were found in the chose folder"
            Exit Sub
        End If
        
        Do While Len(csvNAME) > 0                                   'keep repeating til no more files found
            Set wbCSV = Workbooks.Open(csvPATH & csvNAME)             'open the found dile
            LR = Range("A" & Rows.Count).End(xlUp).Row              'last row of data
            MyArr = Range("A1:N" & LR).Value                        'read the data into memory
            ReDim NewArr((WorksheetFunction.Count(Range("A1:N" & LR)) + 1000), 2)   'create output array
            NewArr(0, 1) = "Date": NewArr(0, 2) = "Value"           'add headers
            NR = 1                                                  'first empty row in new array
        
            For i = 2 To UBound(MyArr, 1)                           'realign old array into new array
                For j = 3 To 14
                    If IsDate(MyArr(1, j) & " " & MyArr(i, 2) & ", " & MyArr(i, 1)) Then
                        NewArr(NR, 1) = Format(DateValue(MyArr(1, j) & " " & MyArr(i, 2) & ", " & MyArr(i, 1)), "MM/DD/YYYY")
                        NewArr(NR, 2) = MyArr(i, j)
                        NR = NR + 1
                    End If
                Next j
            Next i
        
            Sheets.Add                                              'create an output sheet
            Range("A1:C" & UBound(NewArr)).Value = NewArr           'write out the new array
            Range("A:A").Delete xlShiftToLeft                       'cleanup
            Range("A:B").Sort Range("A2"), xlAscending, Header:=xlYes
            ActiveSheet.Move                                        'move the sheet to a new workbook and save it as CSV-NEW
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:=Replace(wbCSV.FullName, ".csv", "-NEW.csv"), FileFormat:=xlCSV, CreateBackup:=False
            ActiveWorkbook.Close False                              'close the created file
            wbCSV.Close False                                       'close the found file
        
            csvNAME = Dir                                           'get the next filename
        Loop
    End Sub
    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