+ Reply to Thread
Results 1 to 4 of 4

split values in cells into new row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-08-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    196

    split values in cells into new row

    I am writing to seek help, in how I can split values in certain cells, in many different rows.

    For example, I would like to split values, into new row entry which are represented as the following:
    Value1 & value & value3
    Value4 & value, value8

    Desired output:
    Value1
    Value
    Value3
    Value4
    Value…. So on.

    I have also attached a sample data example, showing input data in sheet1 and desired output in sheet2, for further reference.
    test_v2.xls

    any further help would be very much appreciated. Thank you

  2. #2
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: split values in cells into new row

    Hi..

    Try this..

    Result shown on sheet3..

    Private Sub CommandButton1_Click()
        Dim x, y, b, cnt As Long, k As Long, s As Long, j As Long, i As Long, ii As Long, LR As Long
        With Range("A1").CurrentRegion
            .Replace "&", ","
            x = .Value
            cnt = 1: k = 1: s = 1
            LR = Range("A" & Rows.Count).End(xlUp).Row
            b = Evaluate("=SUMPRODUCT(LEN(A1:A" & LR & ")-LEN(SUBSTITUTE(A1:A" & LR & ",CHAR(44),"""")))") + LR
            ReDim y(1 To b, 1 To 4)
            For i = 1 To b
                For j = 0 To UBound(Split(x(cnt, 1), Chr(44)))
                    For ii = 1 To UBound(x, 2)
                        If ii = 1 Then
                            y(k, ii) = Trim(Split(x(cnt, 1), Chr(44))(j))
                        ElseIf ii = 3 Then
                            If UBound(Split(x(cnt, 3), Chr(44))) > 0 Then
                                y(k, ii) = Trim(Split(x(cnt, 3), Chr(44))(j))
                            Else
                                y(k, ii) = x(cnt, 3)
                            End If
                        Else
                            y(k, ii) = Trim(x(cnt, ii))
                        End If
                    Next ii
                    If s = UBound(Split(x(cnt, 1), Chr(44))) + 1 Then
                        k = k + 1: cnt = cnt + 1: Exit For
                    Else
                        k = k + 1: s = s + 1
                    End If
                Next j
                s = 1
                If k > b Then
                    With Sheets("Sheet3")
                        .Range("A1").Resize(UBound(y), 4).Value = y
                        .Columns(4).NumberFormat = "mm/dd/yyyy"
                        .Columns.AutoFit
                        .Select
                    End With
                    Exit Sub
                End If
            Next i
        End With
    End Sub
    btw.. I replaced your "&" character with a comma so you had a common delimiter to work with..
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: split values in cells into new row

    Another Approach:

    Sub missy22()
    Application.ScreenUpdating = False
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = LastRow To 1 Step -1
        Namess = Replace(Cells(i, 1).Value, ", ", " & ")
        Money = Replace(Cells(i, 3).Value, ", ", " & ")
        
        If InStr(1, Namess, "&") Then
            For j = UBound(Split(Namess, " & ")) To 0 Step -1
                Rows(i + 1).EntireRow.Insert
                Rows(i).Copy Rows(i + 1)
                Cells(i + 1, 1) = Split(Namess, " & ")(j)
                If InStr(1, Money, "&") Then Cells(i + 1, 3) = Split(Money, " & ")(j)
            Next
            Rows(i).EntireRow.Delete
        End If
    Next
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: split values in cells into new row

    And yet another:

    Sub Missy(): Dim S As String, X, Z, A As Range, C As Range
    Dim e As Long, i As Long, u As Integer, n As Integer, m As Integer
    e = Range("A" & Rows.Count).End(xlUp).Row
    For i = e To 1 Step -1: Set A = Range("A" & i): Set C = Range("C" & i)
    S = Replace(A, ",", "&"): Z = Split(S, "&"): u = UBound(Z)
    S = Replace(C, ",", "&"): X = Split(S, "&"): A = Z(0): C = X(0)
                            If u Then
                For n = 1 To u: A.Offset(n).EntireRow.Insert
                If UBound(Z) = UBound(X) Then m = n Else m = 0
        A.Offset(n, 0) = Z(n): A.Offset(n, 1) = A.Offset(0, 1)
        A.Offset(n, 2) = X(m): A.Offset(n, 3) = A.Offset(0, 3)
                Next n
                            End If: Next i: End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ 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 values in one cell into new cells
    By kamelkid2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-22-2013, 10:32 AM
  2. Replies: 3
    Last Post: 01-13-2012, 08:20 AM
  3. Split String into Cells with incased values []
    By ezykiwi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2008, 07:52 AM
  4. [SOLVED] Split values in one cell and place them in different cells
    By Nagesh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-22-2005, 06:35 AM
  5. Split values from one cell to dif. cells
    By saziz in forum Excel General
    Replies: 2
    Last Post: 09-30-2005, 11:47 AM

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