[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How do I split one string into two?

Posted on 2010-04-09
10
Medium Priority
?
343 Views
Last Modified: 2013-11-25
Hello,

Please see the attached Excel file.  Using VB code, I want to be split each string in column 'A' into 2 separate strings.  The seperator for the two strings could be the '>' symbol.  Any idea how to impliment this?

Thanks,

mvem
Example.xlsx
0
Comment
Question by:mvem
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 30266350
A simple VBA text to columns

hth

Dave

Sub Macro1()
Columns("A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, OtherChar _
        :=">"
End Sub

Open in new window

0
 
LVL 52

Expert Comment

by:Mark Wills
ID: 30266842
Think dave has given you the answer, but I was wondering, why does it have to be VB code ?
0
 

Author Comment

by:mvem
ID: 30268619
Hmm, just tried this and it didn't split anything up.  I was expecting for there two be three columns (A,B and C).  'A' would contain the original string, and 'B' and 'C' would contain the splits.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

Expert Comment

by:Dave Brett
ID: 30269875
If you do have > in the string then the split will occur in A&B

This version, which includes a working file version puts the split in B and C. Click the button to run

Dave

Sub Macro1()
Columns("B").Insert
Columns("A").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, OtherChar _
        :=">"
End Sub

Open in new window

FIle.xls
0
 

Author Comment

by:mvem
ID: 30270477
Hey Dave,

Please see attached file for results.  An exact copy of column 'A' is placed into column 'B'.

mvem
Result.jpg
0
 
LVL 6

Accepted Solution

by:
HyperBPP earned 500 total points
ID: 30270586

Sub splitA()
    On Error Resume Next
    Dim index As Long
    Dim tempArray() As String
    
    With ActiveSheet.UsedRange.Columns(1)
        For index = 1 To .Cells.Count
            tempArray = Split(.Cells(index), " ")
            ActiveSheet.Range("B" & index).Value = tempArray(0)
            ActiveSheet.Range("C" & index).Value = tempArray(1)
        Next
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:mvem
ID: 31760942
Works like a charm...thank you.
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 30270877
Pls substitute this
  Columns("A").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, Other:=True, OtherChar _
        :=">"
in place of
Columns("A").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, OtherChar _
        :=">"

Dave

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 30270955
While this is closed, this is not a job for a Loop. It is a inefficient method given there are superior alternatives

Regards

Dave
0
 
LVL 52

Expert Comment

by:Mark Wills
ID: 30274502
And interesting to see no response to "why VB"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question