列表框最后一行问题之间的项目转移

时间:2018-03-21 15:35:32

标签: vba listboxitem

我有一个列表框,其中包含我想要移动到另一个列表框的值,以便用户可以对项目进行排序。选择最后一个项目时,它仅移动该项目并删除其上方的值。我怎样才能使它像列表中的其他项一样起作用?

仅供参考,它是单项选择,如果改变了任何内容

Option Explicit
Option Base 1

Private Sub Add_Click()
 Dim x As Integer, count As Integer
  count = Me.Unsorted.ListCount
    For x = 0 To count
        If Me.Unsorted.Selected(x) = True Then
            Me.Sorted.AddItem Me.Unsorted.List(x)
        End If
    Next x

    For x = count To 0 Step -1
        If Me.Unsorted.Selected(x) = True Then
            Me.Unsorted.RemoveItem x
        End If
    Next x

End Sub

1 个答案:

答案 0 :(得分:0)

你可以用这个

Private Sub Add_Click()
    Dim x As Integer
    Dim nSelecteds As Long

    With Me
        With .Unsorted
            ReDim selecteds(1 To .ListCount) As Long
            For x = .ListCount - 1 To 0 Step -1
                If .Selected(x) Then
                    nSelecteds = nSelecteds + 1
                    selecteds(nSelecteds) = x
                    Me.Sorted.AddItem .List(x)
                End If
            Next x
            If nSelecteds > 0 Then
                For x = 1 To nSelecteds
                    .RemoveItem selecteds(x)
                Next x
            End If
        End With
    End With
End Sub