+ Reply to Thread
Results 1 to 4 of 4

Split multiple Delimiter field with Header into Multiple Columns and Rows

Hybrid View

  1. #1
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Split multiple Delimiter field with Header into Multiple Columns and Rows

    Try
    Sub test()
        Dim a, i As Long, ii As Long, dic As Object, AL As Object
        Dim m As Object, n As Long, myItems As Long, e, s
        Set dic = CreateObject("Scripting.Dictionary")
        Set AL = CreateObject("System.Collections.ArrayList")
        a = Sheets("sheet1").Cells(1).CurrentRegion.Value
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "([^~:]+) *:([^~]+)"
            For i = 2 To UBound(a, 1)
                If Not dic.exists(a(i, 2)) Then
                    Set dic(a(i, 2)) = CreateObject("Scripting.Dictionary")
                End If
                n = 0
                For ii = 3 To UBound(a, 2)
                    If a(i, ii) <> "" Then
                        n = n + 1
                        Set dic(a(i, 2))(n) = CreateObject("Scripting.Dictionary")
                        If .test(a(i, ii)) Then
                            For Each m In .Execute(a(i, ii))
                                If Not AL.Contains(m.submatches(0)) Then AL.Add m.submatches(0)
                                dic(a(i, 2))(n)(m.submatches(0)) = m.submatches(1)
                            Next
                        End If
                        myItems = myItems + dic(a(i, 2))(n).Count
                    End If
                Next
            Next
        End With
        ReDim a(1 To myItems, 1 To AL.Count + 3): n = 1
        With Sheets("sheet1")
            a(n, 1) = .Cells(1, 1).Value: a(n, 2) = .Cells(1, 2).Value
            a(n, 3) = Split(.Cells(1, 3).Value)(0)
        End With
        For i = 0 To AL.Count - 1: a(1, i + 4) = AL(i): Next
        For Each e In dic
            For i = 0 To dic(e).Count - 1
                n = n + 1: a(n, 1) = n - 1: a(n, 2) = e: a(n, 3) = e & Format$(i + 1, "-000")
                For Each s In dic(e).items()(i).keys
                    a(n, AL.IndexOf(s, 0) + 4) = "'" & dic(e).items()(i)(s)
                Next
            Next
        Next
        With Sheets("sheet2").Cells(1).Resize(n, UBound(a, 2))
            .CurrentRegion: .ClearContents
            .Value = a: .Parent.Select
        End With
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Split multiple Delimiter field with Header into Multiple Columns and Rows

    Try this:-
    Results sheet2
    Sub MG27Mar25
    Dim Ray, Rng As Range, c As Long, Sp As Variant, Ac As Long, Rw As Long
    Dim n As Long, Num As Long
    Ray = Range("A1").CurrentRegion
    ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 2), 1 To 8)
    nray(1, 1) = "RowID": nray(1, 2) = "LocationID": nray(1, 3) = "EmployeeID":
    nray(1, 4) = "First Name": nray(1, 5) = "Last Name": nray(1, 6) = "DOB": nray(1, 7) = "Gender": nray(1, 8) = "Salary Range"
    c = 1
    For Rw = 2 To UBound(Ray, 1)
    Num = 1
     For Ac = 3 To UBound(Ray, 2)
          If Not Ray(Rw, Ac) = vbNullString Then
            c = c + 1
            nray(c, 1) = c - 1: nray(c, 2) = Ray(Rw, 2): nray(c, 3) = Ray(Rw, 2) & "-" & Format(Num, "000")
            Sp = Split(Ray(Rw, Ac), "~")
            Num = Num + 1
            For n = 0 To UBound(Sp)
                nray(c, n + 4) = Split(Sp(n), ":")(1)
            Next n
          End If
      Next Ac
    Next Rw
    With Sheets("Sheet2").Range("A1").Resize(c, 8)
        .Value = nray
        .Columns.AutoFit
    End With
    End Sub
    Regards Mick

+ 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] Split text and number field with no delimiter
    By lasario in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-14-2014, 07:06 PM
  2. split multiple semicolon separated values into new rows for multiple columns
    By bruno08102013 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 01-05-2014, 05:27 PM
  3. Calculate field based on multiple columns and rows
    By Fade-e in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 07-30-2013, 02:27 AM
  4. [SOLVED] Split column into multiple columns at predetermined rows
    By nukeemaway in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-30-2012, 09:32 AM
  5. Replies: 1
    Last Post: 01-17-2006, 08:10 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