+ Reply to Thread
Results 1 to 16 of 16

Multi-line excel cells into rows

Hybrid View

  1. #1
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Multi-line excel cells into rows

    Hi,

    I have three columns which contain multi-line cells, i.e. text separated by ALT+ENTER. Lines of these cells are grouped with each other, i.e. line 1 in cell A2 match with line 1 in B2 and C2. Then I have columns from onwards which have info in the first line which is attributable to all lines in the specific row.

    I now want to turn these multi-line cells from columns A to C to multiple rows and copy the data of columns D to onwards to every new row.

    Hopefully my explanation was not too complicated.

    -Valt
    Last edited by walimos; 08-03-2012 at 09:19 AM.

  2. #2
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    Previous data deleted.
    Last edited by walimos; 08-04-2012 at 03:48 AM.

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Multi-line excel cells into rows

    Is this data attached in post 2 containing real information? It looks so. In that case, please remove it and attach a new file with dummy data.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    Pls find dummy sheet and desired output sheet enclosed.
    Attached Files Attached Files

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Multi-line excel cells into rows

    This should work

    Sub SplitCellsIntoRows()
        Const col_LONGNAME As String = "A"
        Const col_APPREVIATION As String = "B"
        Const col_COLOUR As String = "C"
        
        Dim arrName() As String
        Dim arrAbbr() As String
        Dim arrColour() As String
        
        Dim r As Long
        Dim m As Long
        Dim i As Long
        Dim n As Long
        
        Dim ws As Worksheet
        Set ws = ActiveSheet
        
        m = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For r = m To 2 Step -1
            With ws
                arrName = Split(.Range(col_LONGNAME & r), Chr(10))
                arrAbbr = Split(.Range(col_APPREVIATION & r), Chr(10))
                arrColour = Split(.Range(col_COLOUR & r), Chr(10))
            End With
            n = Application.Max(UBound(arrName), UBound(arrAbbr), UBound(arrColour))
            
            ReDim Preserve arrName(n)
            ReDim Preserve arrAbbr(n)
            ReDim Preserve arrColour(n)
            For i = n To 1 Step -1
                With ws
                    .Range(col_LONGNAME & (r + 1)).EntireRow.Insert
                    .Range(col_LONGNAME & (r + 1)) = Trim(arrName(i))
                    .Range(col_APPREVIATION & (r + 1)) = Trim(arrAbbr(i))
                    .Range(col_COLOUR & (r + 1)) = Trim(arrColour(i))
                End With
            Next i
            With ws
                .Range(col_LONGNAME & r) = Trim(arrName(0))
                .Range(col_APPREVIATION & r) = Trim(arrAbbr(0))
                .Range(col_COLOUR & r) = Trim(arrColour(0))
            End With
        Next r
        
        For r = 2 To ws.Cells(Rows.Count, "C").End(xlUp).Row
            With ws
                If .Cells(r, "D") = "" Then
                    .Cells(r, "D") = .Cells(r - 1, "D")
                    .Cells(r, "E") = .Cells(r - 1, "E")
                End If
            End With
        Next r
    End Sub

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

    Re: Multi-line excel cells into rows

    try
    Sub test()
        Dim a, b(), i As Long, ii As Long, iii As Long, n As Long, x
        With Range("a1").CurrentRegion
            a = .Value
            ReDim b(1 To Rows.Count, 1 To 5)
            On Error Resume Next
            For i = 1 To UBound(a, 1)
                x = Split(a(i, 1), vbLf)
                For ii = 0 To UBound(x)
                    n = n + 1
                    For iii = 1 To 3
                        b(n, iii) = Split(a(i, iii), vbLf)(ii)
                    Next
                    For iii = 4 To UBound(a, 2)
                        b(n, iii) = a(i, iii)
                    Next
                Next
            Next
            On Error GoTo 0
            .Offset(.Rows.Count + 2).Resize(n).Value = b
        End With
    End Sub

  7. #7
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    jindon & mike7952,

    Thank you for your help. I'm still very beginner with Macros. How do I have to adjust the equation/macro if I want to increase the number of rows and columns in my excel sheet. In my actual data set my data expands up to column CL (which of columns A to F have multi-line data) and up to row 11550, number of lines within one column being max 54.

    I just try to understand what are the lines which I have to modify.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Multi-line excel cells into rows

    Can you upload you workbook?

  9. #9
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    Pls find extract of it enclosed. Unfortunately the whole file is too big to be uploaded.
    Attached Files Attached Files

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

    Re: Multi-line excel cells into rows

    try this
    Sub test()
        Dim a, b(), i As Long, ii As Long, iii As Long, n As Long, x
        On Error Resume Next
        Application.DisplayAlerts = False
        Sheets("result").Delete
        Sheets.Add(after:=Sheets(1)).Name = "result"
        
        With Sheets("Activesheet")
            a = .Range("a1", .Range("a" & Rows.Count).End(xlUp)).Resize(, 91).Value
        End With
        ReDim b(1 To Rows.Count, 1 To UBound(a, 2))
        On Error Resume Next
        For i = 1 To UBound(a, 1)
            x = Split(a(i, 1), vbLf)
            For ii = 0 To UBound(x)
                n = n + 1
                For iii = 1 To 6
                    b(n, iii) = Split(a(i, iii), vbLf)(ii)
                Next
                For iii = 7 To UBound(a, 2)
                    b(n, iii) = a(i, iii)
                Next
            Next
        Next
        On Error GoTo 0
        Sheets("result").Cells(1).Resize(n, UBound(b, 2)).Value = b
    End Sub

  11. #11
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    Thnx Jindon,

    It works perfectly with the extract file but gives error when I try to run it with the whole data. Any idea what could be the reason. Error message / bug seems to be in the last row of the code " Sheets("result").Cells(1).Resize(n, UBound(b, 2)).Value = b"

    ---------- Post added at 10:38 AM ---------- Previous post was at 10:35 AM ----------

    'Run-time error '9': Subscript out of range' is the error code.

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

    Re: Multi-line excel cells into rows

    Quote Originally Posted by walimos View Post

    'Run-time error '9': Subscript out of range' is the error code.
    It seems the code doesn't find the sheet named "result"...

    Do you actually see the added sheet named result?

  13. #13
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    Yes I do. But nothing in it.

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

    Re: Multi-line excel cells into rows

    Do you have actually have sheet named "ActiveSheet"?

  15. #15
    Registered User
    Join Date
    08-03-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Multi-line excel cells into rows

    No I didn't have it. It seemed to solve the problem and works like a champ now. Thank you for your help jindon. You saved my day.

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

    Re: Multi-line excel cells into rows

    OK, no problem.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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