Consulting

Results 1 to 9 of 9

Thread: Solved: text to column question

  1. #1
    VBAX Regular
    Joined
    Mar 2012
    Posts
    37
    Location

    Solved: text to column question

    hi,
    i have data in column A that contains a 14 character string that may have 1 or 2 dashes (-) or none at all. i want to be able to separate the data into 2 columns using the last dash on the right.
    so if
    A1 = ABCD-EFG-HIJ, then
    B1 will be ABCD-EFG &
    C1 will be HIJ.


    if A2 = ABCDEF-GHI then
    B2 will be ABCDEF &
    C2 will be GHI

    what formula can i use or can i do this with code?

    thanks
    rem

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    In B1 =IF(SUM(LEN(A1)-LEN(SUBSTITUTE(A1,"-","")))=2,LEFT(A1,LEN(A1)-LEN(C1)-1),TRIM(RIGHT(SUBSTITUTE(TRIM(A1),"-",REPT(" ",50)),50)))

    In C1 =IF(SUM(LEN(A1)-LEN(SUBSTITUTE(A1,"-","")))=2,TRIM(RIGHT(SUBSTITUTE(TRIM(A1),"-",REPT(" ",50)),50)),"")

    More info here and here
    Last edited by mdmackillop; 05-03-2013 at 01:02 PM. Reason: Redundant element deleted
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Mar 2012
    Posts
    37
    Location
    hi mdmackillop,

    the dashes are not always in the same position; how can i adjust the formula?

    thanks again
    rem

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The formulae are not dependent upon absolute position.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Mar 2012
    Posts
    37
    Location
    hi,
    i can't get this to work correctly.
    if there is only one dash, then the cell in column B will contain the data that's after the dash and the cell in column C is left blank.

    rem

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    How about
    in B1 =LEFT(A1, LEN(A1)-LEN(C1)-1)

    and in C1
    =TRIM(RIGHT(SUBSTITUTE(REPT("-",2-MIN(2,(LEN(A1)-LEN(SUBSTITUTE(A1,"-","")))))&A1,"-",REPT(" ","255"),2),255))

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's my test
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I used the array formulae:

    In B1
    =LEFT(A1;MAX((MID(A1;ROW($1:$100);1)="-")*ROW($1:$100))-1)

    In C1
    =MID(A1;MAX((MID(A1;ROW($1:$100);1)="-")*ROW($1:$100))+1;100)

  9. #9
    VBAX Regular
    Joined
    Mar 2012
    Posts
    37
    Location
    thanks everyone for the suggestions. Mike's solution worked the best for me.

    thanks again

    rem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •