+ Reply to Thread
Results 1 to 13 of 13

extract space-separated words in a cell into an array

Hybrid View

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    extract space-separated words in a cell into an array

    If I have a bunch of text in a cell, is it possible to extract each individual word into an array? If so, how would I do it?

    There are never any commas or other symbols in the text - just words separated by a space. So, for example "This is my text" Would be converted into an array ("This", "is", "my", "text")

    My final goal is to put this text into a column in a worksheet so that cell A1 = "This", A2 = "is", and so on.

    Thanks
    You either quit or become really good at it. There are no other choices.

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: extract space-separated words in a cell into an array

    In this example the long string is in B1 of the same sheet as the output:

    Sub luv2glyd()
    
    Dim ws As Worksheet
    Dim myText
    Dim i As Long
    
    Set ws = Worksheets("Sheet1")
    myText = Split(ws.Range("B1").Value, " ") 'assuming the long string is in B1
    For i = 0 To UBound(myText)
        ws.Range("A" & i + 1).Value = myText(i)
    Next i
    
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: extract space-separated words in a cell into an array

    Great! Thank you all who contributed. I was looking for a VBA solution for now, but the rest is good to know for future reference.
    Last edited by luv2glyd; 02-17-2017 at 03:27 PM.

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: extract space-separated words in a cell into an array

    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  5. #5
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: extract space-separated words in a cell into an array

    Here is a formula solution
    Enter formula in B1 and copy across and down
    Formula: copy to clipboard
    =TRIM(MID(SUBSTITUTE(" "&$A1," ",REPT(" ",255)),255*COLUMNS($B:B),255))

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    1
    I have a bunch of text in a cell I have a bunch of text in a cell
    2
    My final goal is to put this text into a column My final goal is to put this text into a column
    Sheet: Sheet3
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: extract space-separated words in a cell into an array

    By VBA, A2:A100 converted in a single line
    Range("A2:A100").TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=True, Space:=True

    Thanks for the Reps

  7. #7
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: extract space-separated words in a cell into an array

    Quote Originally Posted by kev_ View Post
    By VBA, A2:A100 converted in a single line
    Range("A2:A100").TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=True, Space:=True
    Thanks for the Reps
    You're welcome

    Looks like this puts the contents of the array into a row. Is it possible to use something similar to what you have and put it into a single column that is different than the original string's column? So for example, if original string is in cell F5, put the extracted elements into cells A1, A2, A3, A4 and so on?

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: extract space-separated words in a cell into an array

    Nice solution kev... Since every one of the threads I solved today had someone come along knowing better, I think I will stop for a while.

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: extract space-separated words in a cell into an array

    @Arkadi My code is not better - just different.
    But I am a bad typist and fewer characters result in fewer typos
    Found that solution by recording the macro and removing everything that was not required.

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: extract space-separated words in a cell into an array

    Id have to say your code is definitely better
    Your solution is nice, dont apologize or explain yourself.... I just keep missing simpler solutions and got annoyed at myself. Great job with your code... Be proud!
    Last edited by Arkadi; 02-17-2017 at 07:20 PM.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool

    Hi !

    PHP Code: 
    Sub Demo()
        
    SP Split([F5].Value)
        
    Cells(1).Resize(UBound(SP) + 1).Value Application.Transpose(SP)
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  12. #12
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: extract space-separated words in a cell into an array

    Quote Originally Posted by Marc L View Post
    Hi !

    PHP Code: 
    Sub Demo()
        
    SP Split([F5].Value)
        
    Cells(1).Resize(UBound(SP) + 1).Value Application.Transpose(SP)
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

    Nice trick! Thanks.

  13. #13
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: extract space-separated words in a cell into an array

    I would go with @Mark L solution - it is more elegant and flexible than TextToColumns

    If your original data is a row of values, TextToColumns gets a bit clumsy
    - a separate "play" area is required and
    - VBA has to clean up after itself

    eg with values in A1:Z1
    (CurrentRegion needs blank cells around it - hence AB1 not AA1 for "play" area)

    Sub TextToRows()
    
    With Range("AB1")
        Range("A1:Z1").Copy: .PasteSpecial Paste:=xlPasteValues, Transpose:=True
        .Resize(.End(xlDown).Row).TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=True, Space:=True
        .CurrentRegion.Copy: Range("A1").PasteSpecial Paste:=xlPasteValues, Transpose:=True
        .CurrentRegion.Clear
    End With
    
    End Sub
    Solution from @Marl L only needs a loop and a minor mod to achieve the same
    For c = Range("A1").Column To Range("Z1").Column
        ap = Split(Cells(1, c).Value): Cells(1).Resize(UBound(ap) + 1).Offset(, c - 1).Value = Application.Transpose(ap)
    Next c
    Last edited by kev_; 02-18-2017 at 07:19 AM.

+ 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. [SOLVED] How to extract words from a text string separated by comma
    By shukla.ankur281190 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-22-2015, 04:16 AM
  2. [SOLVED] Getting the sum of 2 or 3 numbers within a cell separated by a space
    By JohnnyBoyxxx in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-20-2015, 09:47 AM
  3. [SOLVED] Extract words till last, finding the 5th space from last
    By rajani.indegene in forum Excel General
    Replies: 3
    Last Post: 08-17-2015, 05:39 AM
  4. [SOLVED] Separated first string which is separated by dash (-) or underscore (_) or space ( )
    By nur2544 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-03-2014, 10:32 AM
  5. [SOLVED] splitting a cell that has two values separated by a space
    By Nerpilis in forum Excel - New Users/Basics
    Replies: 6
    Last Post: 06-13-2013, 10:23 PM
  6. Seperating values that are space-separated in one cell to several cells
    By ferferi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-24-2013, 05:58 AM
  7. Extract cell contents separated by either a space, an & or by ,
    By novodisc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2011, 12:08 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