• Moving from listbox to listbox (Word 2000 VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Moving from listbox to listbox (Word 2000 VBA)

    Author
    Topic
    #359247

    I’m in a learning mode, trying to build a form. From time to time I will probably be coming to you guys when I am stuck. When I do, I don’t want you to do the work for me, unless it is absolutely necessary.

    I’m stuck on what I think ought to be simple problem, but I just can’t figure it out.

    I want to assign a sub to the click event of a command button that will move all selected items in list1 to list2.

    Thanks!!
    Troy

    Viewing 0 reply threads
    Author
    Replies
    • #538021

      Edited by jscher2000 on 18-Aug-01 13:38.

      Without actually trying to test this out, I think it would be something like this (you need to replace the stuff in with real object names and properties, of course):

      For intCounter = .ListCount – 1 To 0 Step -1
      If .Selected(intCounter) Then
      .AddItem .List(intCounter)
      .RemoveItem intCounter
      End If
      Next

      I’m counting down to avoid problems that would naturally happen when counting up. Say I removed selected item 2, then 3 would become 2 (etc.), and I’d end up not checking it.

      If you want the items in list2 in an order different from their order in list1, you could first store them in an array and sort them there. Hope this helps.

      • #538024

        I though that worked great!! However, one problem, when I selected the last item in the list and clicked the command button to move them, it moved that item and the item above it, which was NOT selected.

        I thought that may have had something to do with the MultiSelect property (set at the time to “fmMultiSelectSingle”) on my ListBox. So I changed it to “fmMultiSelectExtented” which is what I wanted anyway. Then I found that selecting an item did not activate the command buttons as it had previously. It also did not deselect when I clicked on an item in another list. This was working when I used “fmMultiSelectSingle”. I know this is a different issue than I asked before, but would appreciate any help you could give.

        Also, I want to learn a little more about the code you sent. I may not use the right terminology, so please correct me when I’m wrong.

        intcounter is what, a function, a method? I couldn’t find any info about it on msdn or in the online help for vba. Or is it an ingenious combination of “int” and “counter”?

        It appears to count as you say from the last item in the list (which is the total of the items in the list minus 1 because the ListCount starts at 0. Right?

        Is this the only way to refer to items in a list box? It appears the ListEntry object only applies to drop-down lists. This was probably my biggest hangup. I was trying to use a For Each statement and Dimmed “li” as a ListEntry.

        I think I get the rest. Thanks for taking the time to help and educate a newbie!! doh

        Troy

        • #538025

          Well, I took some shortcuts. smile This code should deal with all of those issues:

          Private Sub CommandButton1_Click()
          Dim intCounter As Integer
          With Me.ListBox1
          If .MultiSelect = fmMultiSelectSingle Then
          ‘use the ListIndex property
          If .ListIndex -1 Then
          ‘something is selected
          Me.ListBox2.AddItem .List(.ListIndex)
          .RemoveItem .ListIndex
          End If
          Else
          ‘multiple selection is possible – use the Selection property
          ‘and check every item
          For intCounter = 0 To .ListCount – 1
          If .Selected(intCounter) Then
          Me.ListBox2.AddItem .List(intCounter)
          End If
          Next
          For intCounter = .ListCount – 1 To 0 Step -1
          If .Selected(intCounter) Then
          .RemoveItem intCounter
          End If
          Next
          End If
          End With
          End Sub

          Private Sub UserForm_Initialize() ‘just for testing
          With Me.ListBox1
          .AddItem “Zero”
          .AddItem “One”
          .AddItem “Two”
          .AddItem “Three”
          .AddItem “Four”
          End With
          End Sub

          Other Q & A:

          • Selection/deselection behavior: I think the “extended” is the best choice, and you can clear any selection by holding ctrl while clicking.
          • intCounter: my standard variable name for an integer used as a loop counter…
          • ListCount: yes, it counts starting from 1 while the listbox rows start at 0.
          • List Property: I see in Help that there is a similar Column property. Otherwise, I don’t know how to read data out of the box.
            [/list]And don’t feel bad about asking
          • #538029

            Moving back and forth between two boxes but keeping the original order was more of a challenge. I’m sure the way I did it is not the simplest or best way, but it seems to work. It might have been faster to go looking on the ‘net, but too late now!

            • #538033

              Thanks for the demo!! I couldn’t tell for sure how you made them sort properly.

              Getting them to go back and forth is no problem now. I actually have 3 lists and I have two buttons between each list to go in either direction. Appropriate buttons are displayed and enabled, displayed and disabled from the click event of each list box.

              Now, I have searched around the vba lounge for a way to have these sort alphabetically. This is an issue because I will be adding random names to these lists and would like them to automatically be sorted. I figure the code could be put in the Change event for the list box. But what I need is the how to sort in a list box (one column, could include letters or digits). I have seen some of your responses in other posts, but they seem far more complex than I need.

              Thanks again for all your help!!
              Troy

            • #538065

              In my demo, I kept a “master list” of them in order in an array, then re-wrote the lists as the user moved them around.

              There is a built-in array sorting function in the WordBasic object; I’m not sure if this ever was turned into a VBA function (none popped up in help). In most cases, you just need to say WordBasic.SortArray myArray(), but if you need to reverse the order or sort by the second dimension in a two dimensional array, then the options can be hard to understand. I created a subprocedure that “wraps” the function in a more useful way (see attached). Even so, the results might not be intuitive. For example, why does the lower case ‘a’ sort before the upper case ‘A’? Anyway, it gives you an idea of how it works. So you could copy everything from the listbox into an array, sort it, and put it back.

              Maybe someone has written a nice routine that works directly on the listbox? In fact, assuming they are alphabetical in the first place, all you really need to do is figure out where to insert the new entry and use the List or Column property to add it at that row position. The “slow” way to do this would be to start at the top and compare the items one by one until you find the right spot. There are faster ways to do this that have names like Quick Sort and Bubble Sort, but I don’t have any code for them. Hopefully someone can help.

    Viewing 0 reply threads
    Reply To: Moving from listbox to listbox (Word 2000 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: