+ Reply to Thread
Results 1 to 17 of 17

Changing the structure of the files

Hybrid View

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

    Question Changing the structure of the files

    Hello members,
    I would appreciate if you could help me to change the structure of all my *.csv files to the following format and write them all in a single .xls file.
    I tried to solve it by myself, but I didn't know even how to record a macro for this specific case.

    INPUT FILE STRUCTURE (*.csv)
    2-15-2015 4-03-41 PM.png

    OUTPUT FILE STRUCTURE
    ** for the dates that there is no value (e.g. 30 & 31 Feb), you can leave it blank or fill it with "?" or "-999" as missing value.
    2-15-2015 4-05-04 PM.png

    Thank you.
    Attached Files Attached Files
    Last edited by Moriexcel; 02-16-2015 at 12:55 AM. Reason: added two more sample files

  2. #2
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Could you post an example CSV file for us to play with - perhaps with dummy data? What is the max number values for any given day? Could it be anything?
    Please consider adding a * if I helped.

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

    Re: Changing the structure of the files

    Quote Originally Posted by Brendan_Floyde View Post
    Could you post an example CSV file for us to play with - perhaps with dummy data? What is the max number values for any given day? Could it be anything?
    Sure, I'll update the main post by adding a sample file.
    The max number of records in different files are not same and could be anything.
    But, the structure of the input files is similar.
    Thank you
    Last edited by Moriexcel; 02-15-2015 at 04:47 AM.

  4. #4
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    As your data is nice and square adodb is way quicker than looping through each line. Requires a reference in the VBE go tools / references then tick microsoft active data objects 6.1 library (or the highest / nearest you have) - you only have to do this once.

    this code assumes 1334108.csv is saved in the same directory as the file with the macro in.

    This just transforms the one input csv file. You mention you have multiple in a folder. It's not clear how you want the data consolidated. Should the values be added together or new line for each input folder? Can you expand the output file example above to show what you want and also perhaps post a second input file?

    Sub LoadCSVtoArray()
    
    strPath = ThisWorkbook.Path & "\"
    
    Set cn = CreateObject("ADODB.Connection")
    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    cn.Open strcon
    
    strSQL = "transform Sum(Value) SELECT id, Year, Day FROM 1334108.csv GROUP BY id, Year, Day PIVOT Month;"
    
    
    Dim rs As Recordset
    Dim rsARR() As Variant
    Dim fldCount As Integer
    Dim iCol As Integer
    Dim iRow As Integer
    
    
    Set rs = cn.Execute(strSQL)
    rsARR = rs.GetRows
    
    fldCount = rs.Fields.Count
    
        For iCol = 1 To fldCount
            Range("a1").Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
        Next
    
    
    rs.Close
    Set cn = Nothing
    
    Range("a2").Resize(UBound(rsARR, 2) + 1, UBound(rsARR, 1) + 1).Value = TransposeDim(rsARR)
    
    End Sub
    Function TransposeDim(v As Variant) As Variant
    ' Custom Function to Transpose a 0-based array (v)
        
        Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
        Dim tempArray As Variant
        
        Xupper = UBound(v, 2)
        Yupper = UBound(v, 1)
        
        ReDim tempArray(Xupper, Yupper)
        For X = 0 To Xupper
            For Y = 0 To Yupper
                tempArray(X, Y) = v(Y, X)
            Next Y
        Next X
        
        TransposeDim = tempArray
    
    End Function
    Last edited by Brendan_Floyde; 02-15-2015 at 07:43 AM.

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

    Re: Changing the structure of the files

    Quote Originally Posted by Brendan_Floyde View Post
    This just transforms the one input csv file. You mention you have multiple in a folder. It's not clear how you want the data consolidated. Should the values be added together or new line for each input folder? Can you expand the output file example above to show what you want and also perhaps post a second input file?
    2-15-2015 8-17-21 PM.png
    is it clear? No header no blank row in between.
    Just added another sample file to the original post.
    Thank you.
    Last edited by Moriexcel; 02-15-2015 at 08:47 AM.

  6. #6
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Just to clarify in your output example the column headings Val1 / val2 are effectively the months?

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

    Re: Changing the structure of the files

    Quote Originally Posted by Brendan_Floyde View Post
    Just to clarify in your output example the column headings Val1 / val2 are effectively the months?
    Yes they are.

  8. #8
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Amended the above for multiple files. Opens a dialogue box for you to select the files in the folder then cycles through and dumps them at the bottom of the table each time. There is no error checking - assumes all the input files are identical.

    Sub LoadCSVtoArray()
    Dim stFilename() As Variant
    Dim stFilenamez As String
    Dim a As Integer
    
    
    stFilename() = Application.GetOpenFilename("Csv Files (*.csv), *.Csv", , "Open the .csv file to merge", , True)
            
    For a = 1 To UBound(stFilename())
    
    strfilenamez = Split(stFilename(a), "\")(UBound(Split(stFilename(a), "\")))
    
    strPath = Replace(stFilename(a), strfilenamez, "", 1)
    
    Set cn = CreateObject("ADODB.Connection")
    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    cn.Open strcon
    
    strSQL = "transform Sum(Value) SELECT id, Year, Day FROM " & strfilenamez & " GROUP BY id, Year, Day PIVOT Month;"
    
    
    Dim rs As Recordset
    Dim rsARR() As Variant
    Dim fldCount As Integer
    Dim iCol As Integer
    Dim iRow As Integer
    
    
    Set rs = cn.Execute(strSQL)
    rsARR = rs.GetRows
    
    fldCount = rs.Fields.Count
    
        For iCol = 1 To fldCount
            Range("a1").Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
        Next
    
    
    rs.Close
    Set cn = Nothing
    
    Cells(GetLastRow("Sheet1", "A") + 1, 1).Resize(UBound(rsARR, 2) + 1, UBound(rsARR, 1) + 1).Value = TransposeDim(rsARR)
    
    Next a
    
    End Sub
    Function TransposeDim(v As Variant) As Variant
    ' Custom Function to Transpose a 0-based array (v)
        
        Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
        Dim tempArray As Variant
        
        Xupper = UBound(v, 2)
        Yupper = UBound(v, 1)
        
        ReDim tempArray(Xupper, Yupper)
        For X = 0 To Xupper
            For Y = 0 To Yupper
                tempArray(X, Y) = v(Y, X)
            Next Y
        Next X
        
        TransposeDim = tempArray
    
    End Function
    
    Function GetLastRow(strSheet, strColum) As Long
    
     Dim MyRange As Range
     Dim lngLastRow As Long
    
        Set MyRange = Worksheets(strSheet).Range(strColum & "1")
    
        lngLastRow = Cells(1048576, MyRange.Column).End(xlUp).Row
        
        GetLastRow = lngLastRow
        
         Set MyRange = Nothing
     End Function

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

    Re: Changing the structure of the files

    Thank you it works. BUT, it is working only for two first files in the folder. Debug : Ln 29
    2-15-2015 10-38-51 PM.png

  10. #10
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Could you run it again and when it error hit debug
    then type ?strfilenamez
    This will then tell you the file it errors out on - could you then post that file? Or rerun excluding that file and see it it gets further? I want to work out if it is a particular file that is the issue

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

    Re: Changing the structure of the files

    Quote Originally Posted by Brendan_Floyde View Post
    Could you run it again and when it error hit debug
    then type ?strfilenamez
    This will then tell you the file it errors out on - could you then post that file? Or rerun excluding that file and see it it gets further? I want to work out if it is a particular file that is the issue
    I could't find a place to type the ?strfilenamez; because after that it highlights the error line. By the way, I skipped the file that the program stopped and run for the rest. The program works well. THANK YOU SO MUCH (I already clicked on *).
    However, this file starting with "?" missing values. Some of the files are like this and I will appreciate if you could solve this as well.
    I added this file to the original post as well (1534001.csv).
    Again thank you for the time.

  12. #12
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Got the file - replicated the error....working on it. It is the problem I suspected....
    Last edited by Brendan_Floyde; 02-15-2015 at 04:03 PM.

  13. #13
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Are all the files the same layout, with same headers etc?

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

    Re: Changing the structure of the files

    Quote Originally Posted by Brendan_Floyde View Post
    Are all the files the same layout, with same headers etc?
    Yes. They are all same.

  15. #15
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    Ahh so it's the files where the 1st record value is ? that is causing the issue as MS then thinks that field is text field rather than a value field.?

    Hmm thinking cap on .... sure i've come across this before.....

  16. #16
    Forum Contributor
    Join Date
    01-14-2014
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    240

    Re: Changing the structure of the files

    added an error handling - 1534001 - has 59 initial records of ? . MS (jet) uses the first 10 records to judge the field type so in this instance it thinks it is a text field - hence the issue. There are other fancier ways of adjusting like adjusting the registry so jet looks at the whole column but this is fast and good enough

    Sub LoadCSVtoArray()
    
    On Error GoTo 0
    
    Dim stFilename() As Variant
    Dim stFilenamez As String
    Dim a As Integer
    Dim StrSQLcorr As String
    
    
    stFilename() = Application.GetOpenFilename("Csv Files (*.csv), *.Csv", , "Open the .csv file to merge", , True)
            
    For a = 1 To UBound(stFilename())
    
    strfilenamez = Split(stFilename(a), "\")(UBound(Split(stFilename(a), "\")))
    
    strPath = Replace(stFilename(a), strfilenamez, "", 1)
    
    Set cn = CreateObject("ADODB.Connection")
    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
    cn.Open strcon
    
    
    strsql = "transform Sum([Value])  SELECT id, Year, Day FROM " & strfilenamez & "  GROUP BY id, Year, Day PIVOT Month;"
    
    
    On Error GoTo 0
    
    Dim rs As Recordset
    Dim rsARR() As Variant
    Dim fldCount As Integer
    Dim iCol As Integer
    Dim iRow As Integer
    
    
    On Error GoTo errorh:
    Set rs = cn.Execute(strsql)
    On Error GoTo 0
    
    rsARR = rs.GetRows
    
    fldCount = rs.Fields.Count
    
        For iCol = 1 To fldCount
            Range("a1").Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
        Next
    
    
    rs.Close
    Set cn = Nothing
    
    Cells(GetLastRow("Sheet1", "A") + 1, 1).Resize(UBound(rsARR, 2) + 1, UBound(rsARR, 1) + 1).Value = TransposeDim(rsARR)
    
    Next a
    
    Exit Sub
    errorh:
    
    strsql = "transform Sum([Value])  SELECT id, Year, Day FROM " & strfilenamez & " Where (value  <> '?') GROUP BY id, Year, Day PIVOT Month;"
    Resume
    
    End Sub
    Function TransposeDim(v As Variant) As Variant
    ' Custom Function to Transpose a 0-based array (v)
        
        Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
        Dim tempArray As Variant
        
        Xupper = UBound(v, 2)
        Yupper = UBound(v, 1)
        
        ReDim tempArray(Xupper, Yupper)
        For X = 0 To Xupper
            For Y = 0 To Yupper
                tempArray(X, Y) = v(Y, X)
            Next Y
        Next X
        
        TransposeDim = tempArray
    
    End Function
    
    Function GetLastRow(strSheet, strColum) As Long
    
     Dim MyRange As Range
     Dim lngLastRow As Long
    
        Set MyRange = Worksheets(strSheet).Range(strColum & "1")
    
        lngLastRow = Cells(1048576, MyRange.Column).End(xlUp).Row
        
        GetLastRow = lngLastRow
        
         Set MyRange = Nothing
     End Function

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

    Re: Changing the structure of the files

    Thank you so much. It works.
    Again, thank you for the time.

+ 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 structure of data
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-02-2014, 08:59 AM
  2. Replies: 4
    Last Post: 12-12-2013, 09:49 AM
  3. 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
  4. Cell style - changing ribbon structure
    By zlodiej in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2013, 02:17 PM
  5. Open files within specific file structure
    By GregR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-18-2005, 03:05 PM

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