Consulting

Results 1 to 5 of 5

Thread: Can't use Split correctly

  1. #1
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location

    Can't use Split correctly

    I try to learn the Split function but I am stuck.

    I would like to copya list of IDs (in a string) into a column with one single Id per cells
    example: "1;2;3" becomes:
    1
    2
    3

    I've read something but can't apply it (you don't have to follow this way if you know better)

    String "1; 2; 3; 4; 5; 6;" is in Cells(1,1)
    and the separator ";" indicated in cells (2,1) (can be various like ";" or "," or "|") so I guess it is easier to ask the user to indicate it?


    Sub_Test
    Dim My_Tex As String
    Dim My_Sepa As String
    Dim My_ID() As String
    
    My_Tex = Cells(1, 1)
    My_Sepa = Cells(2, 1)
    My_ID() = Split(My_Tex, My_Sepa)
    
    For i = LBound(My_ID()) To UBound(My_ID())
    
    x= My_ID(i)
       'then I get stuck !
    Next i
    
    End_Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    In a row:

    Sub M_snb()
       cells(1).resize(,3)=split("aa|bb|cc","|")
    End Sub
    In a column
    Sub M_snb()
       cells(1).resize(3)=application.transpose(split("aa|bb|cc","|"))
    End Sub
    The separator in cells(2,1); result in a column
    Sub M_snb()
       cells(1).resize(3)=application.transpose(split(cells(1),cells(2,1)))
    End Sub

    More on split, arrays and writing into worksheets ( malheureusement je n'ai pas traduit la page en français)

    http://www.snb-vba.eu/VBA_Arrays_en.html
    Last edited by snb; 03-03-2015 at 01:13 PM.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    little more wordy

    Option Explicit
    Sub Test()
        Dim i As Long
        Dim x As String
        
        Dim My_Tex As String
        Dim My_Sepa As String
        Dim My_ID() As String
        
        'testing
        Cells(1, 1).Value = "aaaa;bbbb;cccc;dddd;eeee;"
        Cells(2, 1).Value = ";"
        
        My_Tex = Cells(1, 1)
        My_Sepa = Cells(2, 1)
        
        My_ID() = Split(My_Tex, My_Sepa)
        
        'typically 0 to n-1 and since rows start at 1 we add one to it
        For i = LBound(My_ID()) To UBound(My_ID())
            Cells(i + 1, 3).Value = My_ID(i)
        Next I
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    try this
    Sub Test()
        Dim My_Tex As String
        Dim My_Sepa As String
        Dim My_ID() As String
        
        My_Tex = Cells(1, 1)
        My_Sepa = Cells(2, 1)
        My_ID() = Split(My_Tex, My_Sepa)
        
        For i = LBound(My_ID) To UBound(My_ID)
        
            x = My_ID(i)
            Cells(i + 1, "C") = My_ID(i)
        Next i
    End Sub

  5. #5
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location
    Paul/ JKwan : thanks it works and matchs what I started

    snb: thanks also because I learn a lot, and I've got new way to play ... :-)
    by the way, congratulation for your french. If yousometimes need help for translating stuff, I'll be pleased as long as I am not in a crazy rush work period

Posting Permissions

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