|
-
Aug 13th, 2010, 08:26 PM
#1
Thread Starter
Hyperactive Member
[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
-
Aug 13th, 2010, 08:57 PM
#2
Re: Split String at first instance of Delimiter only
try this:
vb Code:
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))
Dim myArr(1) As String
myArr(0) = parts(0)
parts.RemoveAt(0)
myArr(1) = String.Join(" ", parts.ToArray)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 13th, 2010, 10:05 PM
#3
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:
Dim parts As String() = myString.Split(New Char() {" "c}, 2)
-
Aug 14th, 2010, 12:19 PM
#4
Thread Starter
Hyperactive Member
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.
-
Aug 14th, 2010, 12:33 PM
#5
Re: Split String at first instance of Delimiter only
using jm's code:
vb Code:
Dim parts As String() = Tcell.Split(New Char() {" "c}, 2)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 02:14 PM
#6
Thread Starter
Hyperactive Member
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
-
Aug 14th, 2010, 02:17 PM
#7
Re: Split String at first instance of Delimiter only
Dim parts() As String= Tcell.Split(New Char() {" "c}, 2)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 02:44 PM
#8
Thread Starter
Hyperactive Member
Re: Split String at first instance of Delimiter only
Paul, it errors on the = expected end of statement.
-
Aug 14th, 2010, 03:04 PM
#9
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
-
Aug 14th, 2010, 03:08 PM
#10
Re: Split String at first instance of Delimiter only
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 03:27 PM
#11
Re: Split String at first instance of Delimiter only
ok. assuming it might need to cover vba, this should work:
vb Code:
Sub splitCellValue()
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, " ", 2)
ProjNumber = myArr(0)
ProjName = myArr(1)
MsgBox ProjNumber & vbTab & ProjName
Next
End Sub
Last edited by .paul.; Aug 14th, 2010 at 03:45 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 05:15 PM
#12
Thread Starter
Hyperactive Member
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)
-
Aug 14th, 2010, 05:18 PM
#13
Thread Starter
Hyperactive Member
Re: Split String at first instance of Delimiter only
Disregard! Why is it working now when I tried the exact thing a dozen times?
-
Aug 14th, 2010, 05:23 PM
#14
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 05:30 PM
#15
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|