Consulting

Results 1 to 9 of 9

Thread: Copy String up to the 3rd Space

  1. #1

    Copy String up to the 3rd Space

    Hey All,

    I'm trying to take a string and only copy the first 3 words of it (separated by spaces). I'm 99% of the way there, I just can't figure out where to next the rest of my Do While .... Loops.

    [vba]

    Private Sub TrimName(text As String)

    Dim i As Integer
    Dim newText As String
    i = 1

    Do While Mid(text, i, 1) <> " "
    newText = Left(text, i)
    i = i + 1
    Loop

    'this copies the FIRST WORD of my String, I just need the next two

    End Sub

    [/vba]
    Thanks all!

    BMG

  2. #2
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    A quick throw together, there's probably a better way to do it
    [vba]Dim x As String
    Dim y
    x = "THis is a test asdrkj"
    y = Split(x)
    Debug.Print y(0) & " " & y(1) & " " & y(2)
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub FirstThreeWords()
    Dim test
    test = Split(Range("A1"))
    ReDim Preserve test(2)
    MsgBox Join(test, " ")
    End Sub
    [/VBA]
    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'

  4. #4

    Thanks so much

    Wow,

    Thank you so much (both of you). Although after seeing your solution I certainly feel like I was using a hammer to screw in a lightbulb

    Interesting methods that I've never seen before!

    Thank you again,

    BMG

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Happy to help. Just for information, you could solve this with a loop and the InStr function. Find the first space, then search after that for the next and so on. Split is much neater though.
    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'

  6. #6
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Excellent. I knew that Join would be the way to go, but I apparently wasn't using the Redim properly when I initially tried it (and since split will fill the rest of the original string into the last space of the array, without it you'd end up with the original string again)

    I may have uses for this myself at some time, so I decided to expand your solution into a function which will allow for selecting the number of words:
    [VBA]Private Function firstWords(ByVal theString As String, Optional ByVal wordCount As Integer = 1) As String
    Dim test
    Dim totalWordCount As Integer

    test = Split(theString)
    totalWordCount = UBound(test) - LBound(test) + 1
    If wordCount > totalWordCount Then
    wordCount = totalWordCount
    ElseIf wordCount < 1 Then
    firstWords = ""
    Exit Function
    End If
    wordCount = wordCount - 1
    ReDim Preserve test(wordCount)
    firstWords = Join(test, " ")
    End Function
    [/VBA]

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can do it quite simply with a loop. Double spaces would need to be sorted (as they would with the previous solution, now I think about it)

    [VBA]
    Option Explicit
    Sub test()
    Dim txt As String
    txt = "A quick throw together, there's probably a better way to do it"
    MsgBox FirstWords(txt, 3)
    End Sub

    Function FirstWords(Data As String, Words As Long)
    Dim x As Long, i As Long
    'Fix large spaces
    Do
    Data = Replace(Data, " ", " ")
    Loop Until InStr(1, Data, " ") = 0
    'Count spaces
    x = 0
    Do
    x = InStr(x + 1, Range("A1"), " ")
    i = i + 1
    Loop Until i = Words
    'Result
    FirstWords = Left(Data, x)
    End Function

    [/VBA]
    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
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Good catch. I hadn't thought of multiple consecutive spaces.

    btw, I think you meant [vba]x = InStr(x + 1, Range("A1"), " ") [/vba] to be [vba]x = InStr(x + 1, data, " ") [/vba]


    Also, I tried[vba] FirstWords = Left(Data, x - 1)[/vba] to get rid of the final space, but it doesn't work if the word count is less than the supplied number. Is there an easy way to remove the last space?

  9. #9
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Just use Trim on the string to get rid of any spaces on the beginning and end.

Posting Permissions

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