Results 1 to 16 of 16

Changing the structure of a csv file

Threaded View

  1. #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.

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