+ Reply to Thread
Results 1 to 6 of 6

Trouble with Split Function

Hybrid View

cr6196 Trouble with Split Function 09-04-2012, 03:57 PM
Jakobshavn Re: Trouble with Split... 09-04-2012, 04:01 PM
MaczaQ Re: Trouble with Split... 09-04-2012, 04:01 PM
cr6196 Re: Trouble with Split... 09-04-2012, 04:04 PM
mike7952 Re: Trouble with Split... 09-04-2012, 04:06 PM
Cutter Re: Trouble with Split... 09-04-2012, 05:27 PM
  1. #1
    Registered User
    Join Date
    09-01-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Trouble with Split Function

    Hi,

    I am having trouble with the code below. I am trying to use the below code to split up a ticker symbol entered in Sheet1.A1. The format is XXXX:XXX and I am looking to preserve the letters after :

    The code below only returns the stuff before the :

    Any help would be appreciated.


    Sub SplitIt()
    
        Dim s As Variant
        Dim p As Variant
        
        s = Worksheets("Sheet1").Cells(1, 1).Value
        
        p = Split(s, ":")
        
        For Each i In p
        Worksheets("Sheet6").Cells(1, 1).Value = p
        Next
        
        
    
    End Sub

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Trouble with Split Function

    Note that p(0) is the first part and p(1) is the second part

    Worksheets("Sheet6").Cells(1, 1).Value = p(1)
    inserts the part after the colon
    Gary's Student

  3. #3
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Trouble with Split Function

    here you run a loop without changing destination range so you always return last word:
        For Each i In p
        Worksheets("Sheet6").Cells(1, 1).Value = p 'here should be i ?
        Next
    try
    Sub SplitIt()
    
        Dim s As Variant
        Dim p As Variant
        
        s = Worksheets("Sheet1").Cells(1, 1).Value
        
        p = Split(s, ":")
        
        Worksheets("Sheet6").Cells(1, 1).Value = p(0) 'first element in array p
        
        'or
        ' Worksheets("Sheet6").Cells(1, 1).Value = p(1) 'second element in array p
     
    End Sub
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  4. #4
    Registered User
    Join Date
    09-01-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Trouble with Split Function

    Thank you very much. I suspected it was something embarassingly simple.
    Last edited by Cutter; 09-04-2012 at 05:26 PM. Reason: Removed whole post quote

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Trouble with Split Function

    This what you want?

        s = Worksheets("Sheet1").Cells(1, 1).Value
        p = Split(s, ":")
        For i = LBound(p) To UBound(p)
            Worksheets("Sheet2").Cells(i + 1, 1).Value = p(i)
        Next
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  6. #6
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Trouble with Split Function

    @ cr6196

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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