Results 1 to 15 of 15

Thread: [RESOLVED] Split String at first instance of Delimiter only

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Resolved [RESOLVED] Split String at first instance of Delimiter only

    The Data looks like this:
    057359-001 Pip Drt329 Auburndale, Fl (aub) - Pompano Beach, Fl (pob) 1:0 10gbe Lan Phy.
    MyArr(0) ends up "057359-001" which is correct.
    MyArr(1) ends up "Pip" only which is incorrect.
    I need MyArr(1) to bring back the rest of the Data 'everything else after the first space'.
    What do I need to do?

    Code:
           Sub SplitProj()
                Dim myArr() As String
                Dim ProjNum As String
                Dim ProjName As String
            
                For Each Tcell In Sheets("Data").Range("E3:E100").Value
                myArr = Split(Tcell, " ")
                ProjNumber = myArr(0)
                ProjName = myArr(1)
                  
        MsgBox myArr(0) & " " & myArr(1)
    
            Next
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    try this:

    vb Code:
    1. Dim parts As New List(Of String)("057359-001 Pip Drt329 Auburndale, Fl (aub) - Pompano Beach, Fl (pob) 1:0 10gbe Lan Phy.".Split(" "c))
    2. Dim myArr(1) As String
    3. myArr(0) = parts(0)
    4. parts.RemoveAt(0)
    5. myArr(1) = String.Join(" ", parts.ToArray)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Split String at first instance of Delimiter only

    As always, you should be reading the documentation first whenever you have a question. Both the Split function and the String.Split method let you specify the maximum number of parts to split the String into. If you only want to split on the first delimiter then you would specify 2 as the maximum number of parts:
    vb.net Code:
    1. Dim parts As String() = myString.Split(New Char() {" "c}, 2)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    Please bear with me as I am a Novice, and can't seem make either of these suggestions work.
    @ Paul, I don't think I can just dim that one example as the macro is looping through the cells where that info changes. The only thing that remains the same is the first 10 characters are the project numbers. Maybe, if I just put in Tcell.value instead of the Example string I gave you.

    Code:
    Dim parts As New List(Of String)(Tcell.Value.Split(" "c))
    it just seems there should be an easier way.

    @jmcilhinney
    This piece of code errors it's probably me not understanding what I need to edit.
    Code:
    Dim parts As String() = myString.Split(New Char() {" "c}, 2)
    I keep running across things telling me all i need to do is add a ,2 after the delimiter, but it errors and doesn't work.
    Code:
    myArr = Split(Tcell, " ",2)
    I've ran across this example, but I think it is for VB not VBA
    Code:
    myArr = Split(Tcell, " ")(2)
    Also, what does the c in this example mean
    Code:
    myString.Split(New Char() {" "c}, 2)
    Am I supposed to take out the {}'s it errors if they are present.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    using jm's code:

    vb Code:
    1. Dim parts As String() = Tcell.Split(New Char() {" "c}, 2)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    Code:
           Sub whatever()
            Dim myArr() As String
            Dim parts As String()= Tcell.Split(New Char() {" "c}, 2) 'Errors here Expected end of statement.
            Dim ProjNum As String
            Dim ProjName As String
            Dim Tcell As Range
                For Each Tcell In Sheets("Data").Range("E3:E100").Value
                myArr = Split(Tcell, " ")
                ProjNumber = myArr(0)
                ProjName = myArr(1)
                
            MsgBox myArr(0) & " " & myArr(1)
            Next
        End Sub

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    Dim parts() As String= Tcell.Split(New Char() {" "c}, 2)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    Paul, it errors on the = expected end of statement.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Split String at first instance of Delimiter only

    This might produce different errors (I don't have excel referenced)

    Code:
        Sub whatever()
    
            Dim ProjNum As String
            Dim ProjName As String
            Dim Tcell As Range
            For Each Tcell In Sheets("Data").Range("E3:E100").Value
    
                Dim myArr() As String = Tcell.Split(New Char() {" "c}, 2) 'Errors here Expected end of statement.
                myArr = Split(Tcell, " ")
                ProjNumber = myArr(0)
                ProjName = myArr(1)
                MsgBox(myArr(0) & " " & myArr(1))
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    is this a vba question?

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    ok. assuming it might need to cover vba, this should work:

    vb Code:
    1. Sub splitCellValue()
    2.      Dim myArr() As String
    3.      Dim ProjNum As String
    4.      Dim ProjName As String
    5.        
    6.      For Each Tcell In Sheets("Data").Range("E3:E100").Value
    7.         myArr = Split(Tcell, " ", 2)
    8.         ProjNumber = myArr(0)
    9.         ProjName = myArr(1)
    10.         MsgBox ProjNumber & vbTab & ProjName
    11.     Next
    12.  
    13. End Sub
    Last edited by .paul.; Aug 14th, 2010 at 03:45 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    This is VBA, I believe this should work but it doesn't.
    Code:
    myArr = Split(Tcell, " ", 2)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    Disregard! Why is it working now when I tried the exact thing a dozen times?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Split String at first instance of Delimiter only

    i'm not sure, but if vba is imperfect as vb.net is, that could happen but i don't know a simple solution to remedy it. you might get a useful answer in the office development forum.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Split String at first instance of Delimiter only

    haha oops i'm not in Office Develoment.. How did that happen? Sorry.. Cudos for the help though. ;-)

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width