+ Reply to Thread
Results 1 to 5 of 5

Find closest space to the left of specified position and then split

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2013
    Location
    Norway
    MS-Off Ver
    Excel 2011
    Posts
    5

    Find closest space to the left of specified position and then split

    Hi!

    I'm looking for a way to split a cell with text into two cells. The first cell can only contain up to 40 characters, the rest needs to go in the second cell. However, I don't want the text to be split in the middle of a word. So basically, if the text is in cell A1, I need to find the closest space to the left of position 40 in A1, and move anything to the right of this position to B1. Example:

    Original text:
    A1: One green apple and a bucket of small onions that smell nice

    I want to avoid this:
    A1: One green apple and a bucket of small on
    B1: ions that smell nice

    I want to achieve this:
    A1: One green apple and a bucket of small
    B1: onions that smell nice


    Any ideas?

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Find closest space to the left of specified position and then split

    Can do this with a pair of user defined functions

    Function FirstBit(MyRange As Range) As String
    For N = 40 To 2 Step -1
        If Mid(MyRange, N, 1) = " " Then
            FirstBit = Left(MyRange, N - 1)
            Exit Function
        End If
    Next N
    End Function
    
    Function SecondBit(MyRange As Range) As String
    For N = 40 To 2 Step -1
        If Mid(MyRange, N, 1) = " " Then
            SecondBit = Mid(MyRange, N + 1)
            Exit Function
        End If
    Next N
    End Function
    Open the VBA editor by hitting Alt F11.
    Insert a new module with Insert - Module
    Paste in the above function
    Go back to the sheet by hitting Alt F11.

    In B2, enter =FirstBit(A1)
    In C2, enter =SecondBit(A1)

    Remember to save the workbook as a macro enabled workbook .xlsm

  3. #3
    Registered User
    Join Date
    09-24-2013
    Location
    Norway
    MS-Off Ver
    Excel 2011
    Posts
    5

    Re: Find closest space to the left of specified position and then split

    Quote Originally Posted by mrice View Post
    Can do this with a pair of user defined functions

    Function FirstBit(MyRange As Range) As String
    For N = 40 To 2 Step -1
        If Mid(MyRange, N, 1) = " " Then
            FirstBit = Left(MyRange, N - 1)
            Exit Function
        End If
    Next N
    End Function
    
    Function SecondBit(MyRange As Range) As String
    For N = 40 To 2 Step -1
        If Mid(MyRange, N, 1) = " " Then
            SecondBit = Mid(MyRange, N + 1)
            Exit Function
        End If
    Next N
    End Function
    Open the VBA editor by hitting Alt F11.
    Insert a new module with Insert - Module
    Paste in the above function
    Go back to the sheet by hitting Alt F11.

    In B2, enter =FirstBit(A1)
    In C2, enter =SecondBit(A1)

    Remember to save the workbook as a macro enabled workbook .xlsm

    Fantastic, thank you very much! :D

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Find closest space to the left of specified position and then split

    b1 =IF(LEN(A1)<=40,A1,LEFT(A1,FIND("^",SUBSTITUTE(A1," ","^",LEN(LEFT(A1,41))-LEN(SUBSTITUTE(LEFT(A1,41)," ",""))))-1))
    c1 = =TRIM(SUBSTITUTE(A1,B1,""))
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Find closest space to the left of specified position and then split

    Another way using a macro.....
    Sub SplitCellContent()
    Dim rng As Range, cell As Range
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range("A1:A" & lr)
    For Each cell In rng
        n = Len(cell.Value) - Len(WorksheetFunction.Substitute(cell.Value, " ", ""))
        For i = n To 1 Step -1
            If Len(cell.Value) > 40 Then
                m = WorksheetFunction.Find("*", WorksheetFunction.Substitute(cell.Value, " ", "*", i))
                If m <= 40 Then
                    cell.Offset(0, 1).Value = Mid(cell.Value, m + 1, Len(cell.Value) - m)
                    cell.Value = Left(cell.Value, m)
                End If
            End If
        Next i
    Next cell
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

+ 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. Find the closest zero to the left and return its address
    By mz8l18 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-03-2013, 04:43 PM
  2. Replies: 3
    Last Post: 07-01-2013, 11:28 AM
  3. find closest cell on left & right
    By freeurmind in forum Excel General
    Replies: 3
    Last Post: 07-01-2013, 11:02 AM
  4. [SOLVED] Find first space at 30 characters, then looking to the left
    By SimonDorfman in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-16-2013, 04:07 PM
  5. Replies: 2
    Last Post: 02-12-2013, 12: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