+ Reply to Thread
Results 1 to 3 of 3

Thread: splitting multile lines in one cell into columns

  1. #1
    Registered User
    Join Date
    02-10-2012
    Location
    Colchester, Essex
    MS-Off Ver
    Excel 2010
    Posts
    2

    splitting multile lines in one cell into columns

    I have a database with 8000 erecords. Some columns have cells with multiple lines of text, it looks alike a alt+enter carriage return, but there is no 'charachter' for the carriage return. I want to seperate the mulitple lines into unique columns, but can't figure out how to do it.

    I have attached a single line of the database in a zip file - I really only need to separate column 'M' these are contact names and I want to have each name in a separate column.

    Can anyone help, very urgent, will pay small fee if someone can solve this?
    Thanks in advance.

    Martin Smith
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: splitting multile lines in one cell into columns

    The contact names appear to be in column K - you can split these type of entries with Chr(10)
    Sub spltData()
    Dim ws1 As Worksheet, ws2 As Worksheet, k As Long
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Dim spltStr As String, splNew
    For k = 2 To ws1.Cells(Rows.Count, "K").End(xlUp).Row
    spltStr = ws1.Range("K" & k).Value
    splNew = Split(spltStr, Chr(10))
    ws2.Range("A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row + 1).Resize(, UBound(splNew)) = splNew
    Next
    End Sub
    See attached workbook
    Attached Files Attached Files
    Last edited by smuzoen; 02-11-2012 at 05:28 AM.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    London
    Posts
    294

    Re: splitting multile lines in one cell into columns

    Only other comment here would be that it sometimes depends on how the data is entered in the sheet in the first place; if it is being entered directly then using the split function as Anthony has shown will work fine. But if your data is imported, added via code or otherwise manipulated it can sometimes also have chr(13) characters in it. You could put in another check for chr(13) as shown or you could "manually" check for both like this:

    Private Sub ExtractLines()
    Dim intLastCol As Integer, intNewLinePosition As Integer, intStart As Integer
    Dim lngLastRow As Long, lngRow As Long
    Dim strFullText As String, strSingleLine As String
    
        lngLastRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
        For lngRow = 1 To lngLastRow
            intColOffset = 0
            intStart = 1
            strFullText = Cells(lngRow, ActiveCell.Column).Text
            intNewLinePosition = InStr(intStart, strFullText, Chr(10), vbTextCompare)
            While intNewLinePosition > 0
                intStart = intNewLinePosition - 1
                strSingleLine = Left(strFullText, intStart)
                If InStr(1, strSingleLine, Chr(13), vbTextCompare) > 0 Then strSingleLine = Left(strFullText, intStart - 1)
                intLastCol = Cells(lngRow, Columns.Count).End(xlToLeft).Column
                Cells(lngRow, intLastCol + 1).Value = strSingleLine
                strFullText = Right(strFullText, Len(strFullText) - intNewLinePosition)
                intNewLinePosition = InStr(intStart, strFullText, Chr(10), vbTextCompare)
                If strFullText <> "" And strFullText <> Chr(10) And strFullText <> Chr(13) And intNewLinePosition = 0 Then Cells(lngRow, intLastCol + 2).Value = strFullText
            Wend
        Next lngRow
    
    End Sub
    The above is doing "manually" what the Split function does and checks for both chr(10) and chr(13). To run: paste it into the sheet with your data, select the column you want to split out and it will write the data to the next available column in that row (which means you can select any column and split out whatever is in multiple lines across your sheet).

    Hope that helps.
    MatrixMan.
    --------------------------------------
    Noli nothis permittere te terere.

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