Results 1 to 14 of 14

Thread: sort and create new sheets from CSV data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hello,

    Give the following a try, it will prompt you for the .csv file at runtime. If you have any questions or wish to make any modifications, don't hesitate to ask!

    Sub Amnicbra()
        Dim vArr() As String, vGroupNames() As String, vGNCnt As Long
        Dim i As Long, j As Long, c As Long, r As Long, iUB As Long, jUB As Long
        Dim vFile As String
        vFile = Application.GetOpenFilename("CSV Files,*.csv,Text Files,*.txt,All Files,*.*")
        If LCase(vFile) = "false" Then Exit Sub
        vArr = TextFileToStringArray("c:\pipe.txt", ",")
        vGNCnt = 0
        iUB = UBound(vArr, 1)
        jUB = UBound(vArr, 2)
        ' Workbooks.Add 1
        ' ActiveSheet.Name = "All data"
        ' Range("A1").Resize(iUB + 1, jUB + 1) = vArr
        ReDim vGroupNames(0)
        For i = 0 To iUB
            If InSArr(vGroupNames, vArr(i, 3)) = -1 Then
               ReDim Preserve vGroupNames(vGNCnt)
                vGroupNames(vGNCnt) = vArr(i, 3)
                vGNCnt = vGNCnt + 1
            End If
        Next 'i
        vGNCnt = vGNCnt - 1
        Application.ScreenUpdating = False
        For j = 0 To vGNCnt
            Sheets.Add
            r = 1
            For i = 0 To iUB
                If vArr(i, 3) = vGroupNames(j) Then
                    For c = 1 To jUB + 1
                        Cells(r, c) = vArr(i, c - 1)
                    Next 'c
                    r = r + 1
                End If
            Next 'i
        Next 'j
        Application.ScreenUpdating = True
    End Sub
    
    Public Function TextFileToStringArray(ByVal vFileName As String, _
      Optional ByVal vDelim As String = ",") As String()
        Dim vFF As Long, vFileCont() As String, vTempStr As String, vTempArr, vTempArr2
        Dim LineCt As Long, ColCt As Long, i As Long, j As Long
        vFF = FreeFile
        LineCt = 0
        ReDim vTempArr2(LineCt)
        Open vFileName For Input As #vFF
        Do Until EOF(vFF)
            Line Input #vFF, vTempStr
            vTempArr = Split(vTempStr, vDelim)
            ReDim Preserve vTempArr2(LineCt)
            vTempArr2(LineCt) = vTempArr
            If UBound(vTempArr) > ColCt Then ColCt = UBound(vTempArr)
            LineCt = LineCt + 1
        Loop
        Close #vFF
        LineCt = LineCt - 1
        ReDim vFileCont(LineCt, ColCt)
        For i = 0 To LineCt
            For j = 0 To UBound(vTempArr2(i))
                vFileCont(i, j) = vTempArr2(i)(j)
            Next 'j
        Next 'i
        TextFileToStringArray = vFileCont
    End Function
    
    Function InSArr(ByRef vArray() As String, ByVal vItem As String) As Long
        Dim i As Long, iUB As Long
        iUB = UBound(vArray)
        For i = 0 To iUB
            If vArray(i) = vItem Then
                InSArr = i
                Exit Function
            End If
        Next 'i
        InSArr = -1
    End Function

    Matt
    Last edited by Aussiebear; 10-24-2024 at 01:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •