Results 1 to 5 of 5

Thread: Split Function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Arrow Split Function

    I know how to use the split function... but I'm using it differently this time.. I want to be able to load a whole list in one listbox.. the format of what I am loading goes like this
    aa - bb
    cc - dd
    ff - hh
    and so on..
    After that has been loaded, i want to click ONE button that will split the ENTIRE Listbox putting aa in List1 and bb in List2, cc in List1 and dd in List2, ff in List1 and hh in List2, and so on..
    So, to summarize, i want to split an entire listbox into two listboxes splitting with a - , By pushing one button. Thanks
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  2. #2
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: Split Function

    Load the first listbox with all the info.. then do this.

    VB Code:
    1. Dim i as Integer
    2. dim splitNum as Integer
    3.  
    4. For i = 0 to List1.ListCount - 1
    5.     splitNum = Instr(1, List1.List(i), "-", vbTextCompare) 'find the break
    6.     List2.AddItem Mid(List1.List(i), splitNum + 2) 'add the second part to the second list
    7.     List1.List(i) = Mid(List1.List(i), 1, splitNum - 2) 'rename the "aa - bb" to "aa"
    8. Next i

  3. #3
    Lively Member
    Join Date
    Feb 2005
    Location
    California
    Posts
    97

    Re: Split Function

    Code:
    Private Sub Command1_Click()
    Dim MyStr As String
    Dim MyArray() As String
    Dim MyLoadArray() As String
    Dim x&
    
        ReDim MyArray(0 To (List1.ListCount - 1))
        For x = 0 To List1.ListCount - 1
            Debug.Print List1.List(x)
            MyArray(x) = List1.List(x)
        Next x
        
        List1.Clear
        List2.Clear
        
        For x = 0 To UBound(MyArray())
            ReDim MyLoadArray(0 To 1)
            MyLoadArray() = Split(MyArray(x), " - ", 2, vbTextCompare)
            List1.AddItem MyLoadArray(0)
            List2.AddItem MyLoadArray(1)
            Erase MyLoadArray()
        Next
        Erase MyArray()
    End Sub
    I'm sure there are more ways to do this, but this was the first way I thought of. Hope this helps!

  4. #4
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: Split Function

    I really wish I had VB6 installed on this PC, so I can test some of this stuff. But I've looked that over several times, and it should work just fine.

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Split Function

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim tmp() As String
    3.     For x = 0 To List1.ListCount - 1
    4.         tmp = Split(List1.List(x), " - ")
    5.         List1.List(x) = tmp(0)
    6.         List2.AddItem tmp(1)
    7.     Next
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11.     List1.AddItem "aa - bb"
    12.     List1.AddItem "cc - dd"
    13.     List1.AddItem "ee - ff"
    14.     List1.AddItem "gg - hh"
    15.    
    16. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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