+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Excel Split Single Cell Array into Multiple Cells

  1. #1
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Excel Split Single Cell Array into Multiple Cells

    An application I use exports strings of data in an array in one cell, like so:

    "data1"= value1; "data2"= value2; "data3"= value3; "data4"= value4; "data5"= value5;

    I'd like an equation or macro or something that could split it like this:

    ------------------------------------------------------
    |   |  data1  |  data2 |  data3  |  data4  |  data5  |
    ------------------------------------------------------
    | 1 |  value1 | value2 |  value3 |  value4 |  value5 |
    ------------------------------------------------------
    | 2 |  value1 | value2 |  value3 |  value4 |  value5 |
    ------------------------------------------------------
    | 3 |  value1 | value2 |  value3 |  value4 |  value5 |
    ------------------------------------------------------
    .....
    Is this possible?

    Excel 2003 on Windows XP
    Last edited by devinpitcher; 06-08-2011 at 11:17 AM.

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Excel Split Single Cell Array into Multiple Cells

    Hi,

    Are the Value items numeric, alpha or a mixture?

    How many cells are involved - presumably more than the single cell you mention above. And in which case how do you want the second and subsequent cells parsed. Do other cells have exactly the same 'data1', 'data2' items or do they have further unique values like 'data6', 'data7' etc which extends the table further to the right.

    It would help if you could upload a representative example showing the before and after position.

    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Excel Split Single Cell Array into Multiple Cells

    This will do it of a single cell:
    Sub x()
    
    Dim v, w, i As Long
    
    v = Split(Range("A1"), ";")
    
    For i = LBound(v) To UBound(v)
        If Len(v(i)) > 0 Then
            w = Split(v(i), "=")
            Range("A1").Offset(1, i + 1) = Trim(Replace(w(0), """", ""))
            Range("A1").Offset(2, i + 1).Resize(3) = Trim(w(1))
        End If
    Next i
    
    End Sub

  4. #4
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    This is a better representation:

        ----------------------------------------------------------------
        |   |  data0  |  data1  |  data2 |  data3  |  data4  |  data5  |
        ----------------------------------------------------------------
        | 1 |  ARRAY  |  value1 | value2 |  value3 |  value4 |  value5 |
        ----------------------------------------------------------------
        | 2 |  ARRAY  |  value1 | value2 |  value3 |  value4 |  value5 |
        ----------------------------------------------------------------
        | 3 |  ARRAY  |  value1 | value2 |  value3 |  value4 |  value5 |
        ----------------------------------------------------------------
        . . . . and so on.
    This is a sample of the data:

    "search-terms1" = Chinese jade; "search-terms2" = Chinese archers; "search-terms3" = Antique jade; "search-terms4" = Asian jade; "search-terms5" = China history;

  5. #5
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    If possible, an equation would work better for this situation, but a macro could work, if it could do this to all rows in the database.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Excel Split Single Cell Array into Multiple Cells

    Please address Richard's post.

  7. #7
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    Are the Value items numeric, alpha or a mixture? Numbers and letters. No symbols as far as I know.

    How many cells are involved? There are hundred of rows, and there will be a result of 5 columns per row. search-terms1 - search-terms5.

    And in which case how do you want the second and subsequent cells parsed. I'm looking for the result to be 5 new columns, each with the data from the array.

    Do other cells have exactly the same 'data1', 'data2' items or do they have further unique values like 'data6', 'data7' etc which extends the table further to the right. Just data1 - 5, which will actually be called search-terms1 - 5

  8. #8
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    Here is a database sample.
    Attached Files Attached Files

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Excel Split Single Cell Array into Multiple Cells

    Results on sheet2:
    Sub x()
    
    Dim v, w, i As Long, r As Range
    
    For Each r In Sheet1.Range("A2", Sheet1.Range("A" & Rows.Count).End(xlUp))
        With Sheet2.Range("A" & Rows.Count).End(xlUp)(2)
            .Value = r
            .Offset(, 1).Value = r.Offset(, 1)
            v = Split(r.Offset(, 1), ";")
            For i = LBound(v) To UBound(v)
                If Len(v(i)) > 0 Then
                    w = Split(v(i), "=")
                    .Offset(, i + 2) = Trim(Replace(w(1), """", ""))
                End If
            Next i
        End With
    Next r
    
    End Sub

  10. #10
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    Could you embed that into a excel document or something for me? I get an error when I try to run it.

  11. #11
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Excel Split Single Cell Array into Multiple Cells

    Hi,

    Try adding a -1 to the For Loop line in Stephen's code, i.e.

    For i = LBound(v) To UBound(v) - 1
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  12. #12
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    I'm getting "Object required" for this line:

    With Sheet2.Range("A" & Rows.Count).End(xlUp)(2)

  13. #13
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Excel Split Single Cell Array into Multiple Cells

    Hi,

    Does
        With Sheet2.Range("A" & Rows.Count).End(xlUp).Cells(2, 1)
    help?
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  14. #14
    Registered User
    Join Date
    06-08-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Excel Split Single Cell Array into Multiple Cells

    Nope.

    I just made a new Macro, pasted this in:

    Sub x()
    
    Dim v, w, i As Long, r As Range
    
    For Each r In Sheet1.Range("A2", Sheet1.Range("A" & Rows.Count).End(xlUp))
        With Sheet2.Range("A" & Rows.Count).End(xlUp).Cells(2, 1)
            .Value = r
            .Offset(, 1).Value = r.Offset(, 1)
            v = Split(r.Offset(, 1), ";")
            For i = LBound(v) To UBound(v) - 1
                If Len(v(i)) > 0 Then
                    w = Split(v(i), "=")
                    .Offset(, i + 2) = Trim(Replace(w(1), """", ""))
                End If
            Next i
        End With
    Next r
    
    End Sub
    ... and tried to run it. Did I forget something? Do I need to select something before running? Runtime error 424.

  15. #15
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Excel Split Single Cell Array into Multiple Cells

    Hi,

    Are you sure your data is on Sheet2? That's the VBA named Sheet2. This is not necessarily the "Sheet2" tab name or even the second sheet counting from the left in the Excel App.

    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0