+ Reply to Thread
Results 1 to 9 of 9

Splitting cells by non-printable characters

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2008
    Location
    England
    MS-Off Ver
    MS 2013
    Posts
    42

    Splitting cells by non-printable characters

    Morning All,

    I've had a quick look at past posts and there is plenty on splitting cells but I haven't been able to find anything to help me with the following:

    I've pulled address details from a database which have populated into a single field with each line separated by 2 'square' characters. I need to split each line into a separate cell for a mail merge. I've seen these characters before and always assumed they represent a space or 'return' and I believe is an ASCII character?

    If I select a cursor in the cell the lines drop as they should with 1 square character left at the end of each line and it stays this way when I leave the cell.

    I've tried CLEAN & TRIM functions; the first removes the characters, the second doesn't recognise the character at all. The most promising seems the 'Text to Columns' function, setting it as a delimited string, but I don't seem to be able to enter this particular character as the delimiter under the 'other' option.

    Any ideas?

    Many thanks in advance,

    Simon.
    Last edited by Simon1185; 10-16-2009 at 05:34 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Splitting cells by non-printable charaters

    Try this:
    Sub SplitByCharacter()
    Columns("A:A").SpecialCells(xlCellTypeConstants, 2).TextToColumns Destination:=Range("A1"), _
        DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Other:=True, OtherChar:=Chr(10)
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-31-2008
    Location
    England
    MS-Off Ver
    MS 2013
    Posts
    42

    Re: Splitting cells by non-printable charaters

    Thanks JBeaucaire,

    I knew this day would come - I've not had to play with VBA since college!

    I understand to a degree the code you've written; in that I can see you stipulate a range and the conditions of the split, but unfortunately I've never had to apply any VBA in Excel and am not sure how?!

    I had the top cell of a blank row selected, opened VBA, pasted the data into the window of a new module (is this right?) and adjusted the range from A:A and destination range to suit, but when I clicked to run I got a run-time error '1004' stating that my command couldn't be run for multiple selections?

    Thanks for your swift response.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Splitting cells by non-printable charaters

    Ok, simpler version.
    Sub SplitByCharacter()
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Other:=True, OtherChar:=Chr(10)
    End Sub

  5. #5
    Registered User
    Join Date
    10-31-2008
    Location
    England
    MS-Off Ver
    MS 2013
    Posts
    42

    Re: Splitting cells by non-printable charaters

    JBeaucaire,

    Thanks very much, that worked this time, but it has only retrieved the first line of the address. Can I do anything to retrieve the subsequent lines into their own cells?

    I wondered (guessed!) whether I needed to extend the destination range to more than one colum of cells but I tried this and it did not work.

    Can this be done?

    Simon.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Splitting cells by non-printable characters

    Simon, Text To Columns only works on one column of data at a time. You could make this process so much easier by uploading a workbook with this data in it so we can look at it concurrently.

  7. #7
    Registered User
    Join Date
    10-31-2008
    Location
    England
    MS-Off Ver
    MS 2013
    Posts
    42

    Re: Splitting cells by non-printable characters

    JBeaucaire,

    I've attached some data, all but the last colum have been previously selected with a cursor, you'll see what I mean about what it does once you've entered the cell once when you click on the bottom cell.

    Simon.
    Attached Files Attached Files

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Splitting cells by non-printable characters

    Try this instead:
    Sub SplitByCharRowByRow()
    'JBeaucaire  (10/13/2009)
    Dim MyArr, i As Long, c As Long, LR As Long
    Application.ScreenUpdating = False
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LR
        MyArr = Split(Cells(i, "A"), Chr(10))
        For c = 0 To UBound(MyArr)
            Cells(i, 1 + c) = WorksheetFunction.Clean(MyArr(c))
        Next c
    Next i
    
    Cells.WrapText = False
    Cells.Columns.AutoFit
    Application.ScreenUpdating = True
    End Sub

  9. #9
    Registered User
    Join Date
    10-31-2008
    Location
    England
    MS-Off Ver
    MS 2013
    Posts
    42

    Re: Splitting cells by non-printable characters

    JBeaucaire,

    Thanks very much, that has worked perfectly.

    I’ve been a while responding as out of curiosity I played around for a while to work out how to change the destination of the results; my data is in a different column to the data I gave you as I had to remove sensitive information,

    Many thanks again for all your help.

    Simon.

+ 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